PHP新手。以下是显示帖子和分页的代码。我试图在每页上显示10个帖子,并且对于要编写的代码感到困惑。我尝试将阅读设置更改为10篇博文,但是当我保存它时,它会覆盖回一个。所以我认为该设置正在某处被覆盖。我想在这里覆盖它。请帮忙。
我尝试添加: 但不仅会发布10个帖子,还会显示帖子下方的第二个类别列表。
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert">
<?php _e('Sorry, no results were found.', 'roots'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) : ?>
<nav class="post-nav">
<ul class="pager">
<li class="previous"><?php next_posts_link(__('← Older posts', 'roots')); ?></li>
<li class="next"><?php previous_posts_link(__('Newer posts →', 'roots')); ?></li>
</ul>
</nav>
<?php endif; ?>
答案 0 :(得分:0)
尝试将此添加到您的functions.php
function trance_posts_per_page( $query ) {
if (! is_main_query())
return;
$query->set( 'posts_per_page', 20 );
}
add_action( 'pre_get_posts', 'trance_posts_per_page' );
检查插件WP-PageNavi,如果这对您没有帮助
答案 1 :(得分:0)
通过删除它来工作:
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
并将其替换为:
<?php query_posts( $query_string . '&posts_per_page=-10' );?>
<?php while (have_posts()) : the_post(); ?>
<article class="block clearfix">
<?php get_template_part('templates/contentcategory',
get_post_format()); ?>
</article>
<br />
<?php endwhile; ?>