Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向woocommerce添加每页产品下拉列表_Php_Jquery_Wordpress_Woocommerce - Fatal编程技术网

Php 向woocommerce添加每页产品下拉列表

Php 向woocommerce添加每页产品下拉列表,php,jquery,wordpress,woocommerce,Php,Jquery,Wordpress,Woocommerce,我正在尝试在我的woocommerce店面子主题中添加一个“每页产品”下拉列表,而不使用插件。我正在将下面的代码添加到我的functions.php中 add_action('woocommerce_-before_-shop_-loop','ps_-selectbox',25); 函数ps_selectbox(){ $per_page=过滤器输入(输入获取'perpage',过滤器消毒\u编号\u INT); 回声'; 回音“每页:”; 回声'; $orderby_options=数组( '8

我正在尝试在我的woocommerce店面子主题中添加一个“每页产品”下拉列表,而不使用插件。我正在将下面的代码添加到我的functions.php中

add_action('woocommerce_-before_-shop_-loop','ps_-selectbox',25);
函数ps_selectbox(){
$per_page=过滤器输入(输入获取'perpage',过滤器消毒\u编号\u INT);
回声';
回音“每页:”;
回声';
$orderby_options=数组(
'8' => '8',
'16' => '16',
'32' => '32',
'64' => '64'
);
foreach($orderby_选项为$value=>$label){
回显“$label”;
}
回声';
回声';
}
添加操作('pre_get_posts'、'ps_pre_get_products_query');
函数ps\u pre\u get\u products\u query($query){
$per_page=过滤器输入(输入获取'perpage',过滤器消毒\u编号\u INT);
如果($query->is_main_query()&&!is_admin()&&is_post_type_归档('product')){
$query->set('posts\u per\u page',$per\u page);
}
}
当我这样做时,下拉框会显示,但我选择的任何选项都会将我带回主题的首页


有人能帮我吗?

我正在使用下面的代码-

//让我们创建一个函数来存放表单
删除操作('woocommerce\u在商店前\u循环','woocommerce\u目录\u订购',30);
功能目录页面订购(){
?>
您可以将以下代码添加到functions.php文件中。

第一步是在商店存档页面上显示一个选择框。使用一些基本的php,我们可以通过woocommerce\u-before\u-shop\u循环钩子来回显一个选择框

add_action( 'woocommerce_before_shop_loop', 'ps_selectbox', 25 );
function ps_selectbox() {
    $per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);     
    echo '<div class="woocommerce-perpage">';
    echo '<span>Per Page: </span>';
    echo '<select onchange="if (this.value) window.location.href=this.value">';   
    $orderby_options = array(
        '8' => '8',
        '16' => '16',
        '32' => '32',
        '64' => '64'
    );
    foreach( $orderby_options as $value => $label ) {
        echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
    }
    echo '</select>';
    echo '</div>';
}

这是为您的WooCommerce商店添加“每页产品”下拉列表的最简单的方法,您也可以使用ajax方法。

WooCommerce中有简短的代码。您有一个如何使用短代码添加下拉列表的示例吗?
// Lets create the function to house our form
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

function woocommerce_catalog_page_ordering() {
?>
<?php echo '<span class="itemsorder">Items Per Page:' ?>
    <form action="" method="POST" name="results" class="woocommerce-ordering">
    <select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
<?php

//Get products on page reload
if  (isset($_POST['woocommerce-sort-by-columns']) && (($_COOKIE['shop_pageResults'] <> $_POST['woocommerce-sort-by-columns']))) {
        $numberOfProductsPerPage = $_POST['woocommerce-sort-by-columns'];
          } else {
        $numberOfProductsPerPage = $_COOKIE['shop_pageResults'];
          }

//  This is where you can change the amounts per page that the user will use  feel free to change the numbers and text as you want, in my case we had 4 products per row so I chose to have multiples of four for the user to select.
            $shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array(
            //Add as many of these as you like, -1 shows all products per page
              //  ''       => __('Results per page', 'woocommerce'),
                '20'        => __('20', 'woocommerce'),
                '-1'        => __('All', 'woocommerce'),
            ));

        foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
            echo '<option value="' . $sort_id . '" ' . selected( $numberOfProductsPerPage, $sort_id, true ) . ' >' . $sort_name . '</option>';
?>
</select>
</form>

<?php echo ' </span>' ?>
<?php
}

// now we set our cookie if we need to
function dl_sort_by_page($count) {
  if (isset($_COOKIE['shop_pageResults'])) { // if normal page load with cookie
     $count = $_COOKIE['shop_pageResults'];
  }
  if (isset($_POST['woocommerce-sort-by-columns'])) { //if form submitted
    setcookie('shop_pageResults', $_POST['woocommerce-sort-by-columns'], time()+1209600, '/', 'www.your-domain-goes-here.com', false); //this will fail if any part of page has been output- hope this works!
    $count = $_POST['woocommerce-sort-by-columns'];
  }
  // else normal page load and no cookie
  return $count;
}

add_filter('loop_shop_per_page','dl_sort_by_page');
add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_page_ordering', 20 );
add_action( 'woocommerce_before_shop_loop', 'ps_selectbox', 25 );
function ps_selectbox() {
    $per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);     
    echo '<div class="woocommerce-perpage">';
    echo '<span>Per Page: </span>';
    echo '<select onchange="if (this.value) window.location.href=this.value">';   
    $orderby_options = array(
        '8' => '8',
        '16' => '16',
        '32' => '32',
        '64' => '64'
    );
    foreach( $orderby_options as $value => $label ) {
        echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
    }
    echo '</select>';
    echo '</div>';
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
   $per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
   if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'product' ) ){
        $query->set( 'posts_per_page', $per_page );
    }
}