WooCommerce每页产品下拉列表

时间:2013-04-22 作者:Luke Seall

我使用的代码如下-

http://designloud.com/how-to-add-products-per-page-dropdown-to-woocommerce/?showmodaldialog=1#comment-1554

显示下拉选择器,以便用户可以选择每页要查看的产品数量。除了分页之外,其他都可以。选择下一页将返回“找不到页”404错误。我想这和饼干有关。有人能帮忙吗?谢谢

// Lets create the function to house our form
function woocommerce_catalog_page_ordering() {
?>


<form action="" method="POST" name="results">
<select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
<?php

        //  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(
                \'\'       => __(\'Results per page\', \'woocommerce\'),
                \'2\'    => __(\'2 per page\', \'woocommerce\'),
                \'36\'        => __(\'36 per page\', \'woocommerce\'),
                \'48\'        => __(\'48 per page\', \'woocommerce\'),
                \'64\'        => __(\'64 per page\', \'woocommerce\'),
            ));

            foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
                echo \'<option value="\' . $sort_id . \'" \' . selected( $_SESSION[\'sortby\'], $sort_id, false ) . \' >\' . $sort_name . \'</option>\';
        ?>
</select>

</form>
<?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, \'/\', \'beadsnwire.lukeseall.co.uk/\', 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 );

1 个回复
SO网友:Sormano

这与Cookie无关,这就是发生的事情:1。访客进入第4页,共6页,其中显示40-50种产品(每页10种)2。当访问者将每页的产品从10页更改为30页时,他仍将停留在第4页,此时只有2页(60个产品/30=2页)

这将创建404。

要解决此问题,还可以在表单上方添加:

global $wp_query;
$cat = $wp_query->get_queried_object();
$action = ""; // default to current page
if( isset( $cat->term_id ) ) 
    $action = get_term_link( (int)$cat->term_id, "product_cat" );
您应该在表单的前面添加它,并设置操作

<form action="<?php echo $action; ?>" method="POST" name="results">
(希望它完美无缺,只是在这里键入了它)

希望这有帮助!

编辑:我确实做了一个插件来添加“每页产品”下拉列表,这还没有解决,但我会把它放在我的待办事项列表上。(http://wordpress.org/plugins/woocommerce-products-per-page/)

结束

相关推荐

Wordpress Pagination Problem

我想在wordpress索引中设置分页。php,但它不工作。在我的索引中。php有3个显示自定义帖子的循环部分,我想在索引中的Blog/recent post部分循环中设置分页。php。这是索引中的代码。php<?php /** * @package WordPress * @subpackage Adapt Theme */ $options = get_option( \'adapt_theme_settings\' ); ?> &l