You are here:  Home » PHP » 如何在WordPress循环中显示任意数量的帖子

WordPress使用循环显示每个帖子。使用The Loop,WordPress处理要在当前页面上显示的每个帖子,并根据它们与The Loop标签中指定条件的匹配方式对其进行格式化。通常,要显示的帖子数量在“读数”选项卡下的“WordPress管理面板设置”区域中设置。但在本文中,我们将向您展示如何使用超级循环覆盖该数字,这将允许您在该特定WordPress循环中显示任意数量的帖子。这将允许您自定义页面的显示,包括作者配置文件,侧边栏等。

打开一个模板文件,您可以在其中放置帖子,然后只需添加此循环:

// if everything is in place and ready, let"s start the loop
 
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

// to display "n" number of posts, we need to execute the loop "n" number of times
// so we define a numerical variable called "$count" and set its value to zero
// with each iteration of the loop, the value of "$count" will increase by one
// after the value of "$count" reaches the specified number, the loop will stop
// *USER: change the "n" to the number of posts that you would like to display

<?php static $count = 0;
if ($count == "n") { break; }
else { ?>

// for CSS styling and layout purposes, we wrap the post content in a div
// we then display the entire post content via the "the_content()" function
// *USER: change to "<?php the_excerpt(); ?>" to display post excerpts instead

<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>

// here, we continue with the limiting of the number of displayed posts
// each iteration of the loop increases the value of "$count" by one
// the final two lines complete the loop and close the if statement

<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>

和你完成了在设计作者模板时,此代码对您非常有帮助,因为您希望控制每个循环显示帖子数量。

评论被关闭。