我正在建设与店面儿童主题的WooCommerce商店。在我的商店页面(archive-product.php
)中,我使用以下代码来显示产品:
<?php
$filterArray = explode("/", "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI]);
if(count($filterArray)>5) {
echo do_shortcode('[products category="' . $filterArray[count($filterArray)-2] . '" per_page="30" limit="30" columns="3" paginate="true"]');
}
else {
echo do_shortcode('[products per_page="30" limit="30" columns="3" paginate=true]');
}
?>
当我没有将category
属性传递给短代码时,一切都会按预期进行。但是当我添加category
时,我的页面显示的不是30个产品,而是每个页面只显示2个产品。将products
更改为product category
根本不起作用。这种奇怪行为的原因可能是什么呢?
编辑:我的代码中有一些混乱,在functions.php
中发现了以下函数
function modify_product_cat_query( $query ) {
if (!is_admin() && $query->is_tax("product_cat")){
$query->set('posts_per_page', 2);
}
}
add_action( 'pre_get_posts', 'modify_product_cat_query' );
我以为我终于找到问题了,但即使我删除了sinppet或将值从2改为30,类别页面仍然只显示两个产品……
发布于 2021-04-13 20:17:43
试着用这个。您正处于存档页面,因此获取当前术语更容易。您不必从url获取它。
$term = get_queried_object();
if( !empty( $term ) && !is_wp_error( $term ) ) {
echo do_shortcode('[products category="' . $term->slug . '" per_page="6" limit="30" columns="3" paginate="true"]');
} else {
echo do_shortcode('[products per_page="30" limit="30" columns="3" paginate=true]');
}
您面临的问题是,产品显示的内容不超过2个,这可能是因为有一些东西覆盖了posts_per_page。
https://stackoverflow.com/questions/67073800
复制相似问题