我已经try 了下面的URL参数,orderby
参数工作正常,显示所有按日期排序的产品,但我只想显示前3个产品(limit
参数不工作):
../shop/?orderby=date&limit=3
有人成功了吗?
我已经try 了下面的URL参数,orderby
参数工作正常,显示所有按日期排序的产品,但我只想显示前3个产品(limit
参数不工作):
../shop/?orderby=date&limit=3
有人成功了吗?
这可以通过在woocommerce_product_query
操作钩子中挂钩的函数来完成.
posts_per_page
参数:当将3
设置为posts_per_page
参数时,它每页(so not convenient)显示3个产品.
如果我们另外将nopaging
参数设置为FALSE,则显示所有产品时不会分页.
post__in
参数(the right way):这需要使用您定义的"Limit"URL参数预先查询按日期排序的产品ID.前3个产品显示时没有分页.
密码:
add_action( 'woocommerce_product_query', 'enable_query_var_limit', 10, 2 );
function enable_query_var_limit( $query, $q ) {
error_log(print_r($query, true));
if ( ! is_admin() && isset($_GET['limit']) && isset($_GET['orderby'])
&& is_numeric($_GET['limit']) && $_GET['orderby'] === 'date' ) {
$query->set( 'post__in', wc_get_products( [
'status' => 'publish',
'limit' => intval($_GET['limit']),
'orderby' => 'date',
'return' => 'ids',
] ) );
}
}
代码将在子主题的functions.php文件中(或插件中).
测试和工作与?orderby=date&limit=3
查询字符串(URL variables).