我需要将post_per_page设置为仅显示1条帖子,这是我使用的代码:
<?php
$yell = new WP_Query(array('posts_per_page' => 1,'post_type' => 'items', 'portfolio-category' => 'accessories'));
while ($yell->have_posts()) : $yell->the_post();
?>
<h2><?php the_title(); ?></h2>
<?php endwhile; wp_reset_query(); ?>
Run Code Online (Sandbox Code Playgroud)
但它没有显示1个帖子,而是显示了所有帖子。不知道为什么
您可以使用post_limits过滤器执行此操作:
add_filter('post_limits', 'your_query_limit');
function your_query_limit($limit){
return "LIMIT 1";
}
Run Code Online (Sandbox Code Playgroud)
更新:如果只希望针对自定义查询运行此操作,则可以执行以下操作:
代码如下:
add_filter('post_limits', 'your_query_limit');
$yell = new WP_Query(
array(
'post_type' => 'items',
'portfolio-category' => 'accessories'
)
);
remove_filter('post_limits', 'your_query_limit');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6726 次 |
| 最近记录: |