限制 WP_Query 和 WooCommerce 产品查询中的帖子
Limit posts on a WP_Query and in WooCommerce product query
在 Woocommerce 中,我需要用 wp_query
来限制 post。
我知道如何在 wp_query
中限制 post
比如这个页面id是20,我是运行查询:
function wpcodex_filter_main_search_post_limits( $limit, $query ) {
if ( get_the_ID()==20 ) {
return 'LIMIT 0, 12';
}
return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
但我需要将限制设置为 25 到 35。
如何做到这一点?
即使我放置 return 'LIMIT 25, 35';
它仍然显示前 35 个产品,而不是 25 到 35 之间的 posts。
该查询类似于:LIMIT 10 OFFSET 15
请帮助我。
答案是…… taratata!!!! :
function wpcodex_filter_main_search_post_limits( $limit, $query ) {
if ( get_the_ID()==20 ) {
return 'LIMIT 25, 10';
}
return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
因为在'LIMIT 25, 10';
中,这里25
是偏移量,10
是要显示的帖子数。这样一来,它将显示以产品 26 开头的 10 种产品……
查看此线程:Whats the difference between post_limits and pre_get_posts
在 Woocommerce 中,我需要用 wp_query
来限制 post。
我知道如何在 wp_query
中限制 post比如这个页面id是20,我是运行查询:
function wpcodex_filter_main_search_post_limits( $limit, $query ) {
if ( get_the_ID()==20 ) {
return 'LIMIT 0, 12';
}
return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
但我需要将限制设置为 25 到 35。
如何做到这一点?
即使我放置 return 'LIMIT 25, 35';
它仍然显示前 35 个产品,而不是 25 到 35 之间的 posts。
该查询类似于:LIMIT 10 OFFSET 15
请帮助我。
答案是…… taratata!!!! :
function wpcodex_filter_main_search_post_limits( $limit, $query ) {
if ( get_the_ID()==20 ) {
return 'LIMIT 25, 10';
}
return $limit;
}
add_filter( 'post_limits', 'wpcodex_filter_main_search_post_limits', 10, 2 );
因为在'LIMIT 25, 10';
中,这里25
是偏移量,10
是要显示的帖子数。这样一来,它将显示以产品 26 开头的 10 种产品……
查看此线程:Whats the difference between post_limits and pre_get_posts