Wordpress循环显示限制帖子
这是基本循环Wordpress循环显示限制帖子,wordpress,Wordpress,这是基本循环 我想在搜索结果页面上显示20篇文章。我知道我们可以更改管理面板选项上的值,但它会更改所有选项,如索引页和归档页等。我需要使用不同的选项。非常好的参考: 在调用while语句之前,需要查询帖子。因此: <?php query_posts('posts_per_page=20'); ?> <?php while (have_posts()) : the_post(); ?> <!-- Do stuff... --> <?
我想在搜索结果页面上显示20篇文章。我知道我们可以更改管理面板选项上的值,但它会更改所有选项,如索引页和归档页等。我需要使用不同的选项。非常好的参考:
在调用while语句之前,需要查询帖子。因此:
<?php query_posts('posts_per_page=20'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do stuff... -->
<?php endwhile;?>
编辑:很抱歉分页,请尝试以下操作:
<?php
global $query_string;
query_posts ('posts_per_page=20');
if (have_posts()) : while (have_posts()) : the_post();
?>
<!-- Do stuff -->
<?php endwhile; ?>
<!-- pagination links go here -->
<? endif; ?>
您可以通过$wp\u查询对象限制每个循环的帖子数量。 它以多个参数为例:
<?php
$args = array('posts_per_page' => 2, 'post_type' => 'type of post goes here');
$query = new WP_Query( $args );
while( $query->have_posts()) : $query->the_post();
<!-- DO stuff here-->
?>
更多关于wp_查询对象的信息添加'paged'=>$paged分页将有效
<?php
$args = array('posts_per_page' => 2, 'paged' => $paged);
$query = new WP_Query( $args );
while( $query->have_posts()) : $query->the_post();
<!-- DO stuff here-->
?>
在模板内使用新查询的答案将无法正确用于自定义帖子类型 但提供钩住任何查询,检查它是主查询,并在执行之前修改它。这可以在模板函数内完成:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()) {
// alter the query for the home and category pages
if(is_home()){
$query->set('posts_per_page', 3);
}
if(is_category()){
$query->set('posts_per_page', 3);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
我找到了这个解决方案,它对我有效
global $wp_query;
$args = array_merge( $wp_query->query_vars, ['posts_per_page' => 20 ] );
query_posts( $args );
if(have_posts()){
while(have_posts()) {
the_post();
//Your code here ...
}
}
query_posts的codex明确指出分页将不再有效:(我自己仍在寻找解决方案)更正:它只在页面模板上有效。在正常循环中,您必须使用设置>阅读。不适用于我(列表消失),但@nayeri解决方案确实有效(见下文)。