WordPress 主循环 – 如何在指定文章数之前添加class
有没有办法在 WordPress 循环代码中获取多个项目:
<?php while (have_posts()) : the_post(); ?>
这个循环列出了帖子。 但我需要将某些class添加到前 3 篇文章,如何解决?
解决方案
您可以使用$WP_Query
的post_count
属性,如下所示:
$wp_query->post_count
请注意与found_posts
的区别,它计算虽然匹配查询但未显示(例如用于分页)的帖子。 您可能希望根据您的特定情况使用其中一种。
这是一种解决方法:
<?php
$count = 0; //set up counter variable
while (have_posts()) : the_post();
$count++; //increment the variable by 1 each time the loop executes
if ($count<4) {
// here put the special code for first three
}
// here put the code for normal posts
endwhile;
?>
我用这个在我的博客中
<?php $count = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); $count++;?>
<div class="col-lg-3">
<h3><a href="https://stackoverflow.com/questions/19303556/wordpress-loop-how-to-count-items/<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php the_excerpt();?></p>
</div>
<?php if ($count==4) { $count = 0;?>
<div class="clearfix"></div>
<?php } ?>
<?php endwhile; endif; ?>
你可能还喜欢下面这些文章

wpac是一款wordpress自动配图插件,可以丰富文章内容,对提升排名有很大帮助。p style=”font-size:18px;”>你可能还喜欢下面这些文章<p>{excerpt}<

WordPress怎么增加文章排序方式?实现过程也比较简单,一个是构造链接,另外一个是使用query_posts来改变一下主循环就可以了。 $orderby, ‘order’ =>

让WordPress一个页面显示全部分类的文章cat=’ . $cat->php echo $cat->a href=”<$cats = get_categories(array(‘include’ =>