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; ?>
你可能还喜欢下面这些文章

现在写点文章不配图总觉得文章质量不太高。 于是萌生了写一款文章自动配图的插件的想法。 文章页自动加一张图比较简单,从候选集中选择一张图片拼接在正文中即可。 又比如在图片中添加文章标题,摘要或者自定义的文字。

众所周知,规律发文无论对用户还是对搜索引擎都十分友好。 wpap就是是一款能让你的wordpress自动定时发布文章的插件。 首先下载并安装站长工具箱插件。 然后再下载并安装定时发文插件

随着站点的文章越来越多,老旧文章逐渐被埋没,曝光机会减少,流量随之减少。 wppr就是这样一个支持文章自动翻新的插件。 先下载并安装站长工具箱,再下载并安装自动翻新插件。

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

网站获取流量需要依靠长尾词,但我们不可能把所有的长尾词都堆在标题或者正文中。 现在我开发了一款能自动匹配长尾词的插件wpkws,他的功能是找到和文章相似的长尾词,自动添加为标签。

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