WordPress 主循环 – 如何在指定文章数之前添加class

更新于 2022年1月23日 wordpress教程

有没有办法在 WordPress 循环代码中获取多个项目:

<?php while (have_posts()) : the_post(); ?>

这个循环列出了帖子。 但我需要将某些class添加到前 3 篇文章,如何解决?

解决方案

您可以使用$WP_Querypost_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; ?>

你可能还喜欢下面这些文章

蜂集采集器jQuery选择器采集教程蜂集采集器jQuery选择器采集教程

//www.imwpweb.com”>imwpweb</a></div></div></body></html>使用id选择器。

wordpress怎么调用特定文章列表wordpress怎么调用特定文章列表

a href=”< //cat=1为调用ID为1的分类下文章”title=”<

蜂集采集器如何采集标签蜂集采集器如何采集标签

//www.imwpweb.com”>标签二</a>。//www.imwpweb.com”>标签三</a></div>tag是在a标签中,那么jquery选择器可以写成。

wprec推荐插件模板变量文档以及样式推荐wprec推荐插件模板变量文档以及样式推荐

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

WordPress怎么增加文章排序方式WordPress怎么增加文章排序方式

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

wordpress如何在一个页面显示所有文章wordpress如何在一个页面显示所有文章

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

WordPress 添加类别WordPress 添加类别

在本章中,我们将研究如何在WordPress中添加类别。步骤(4) – 点击Add New Category后,新创建的类别将显示在页面的右侧,如以下屏幕所示。

如何自动为WordPress文章添加特色图像如何自动为WordPress文章添加特色图像

add_action(‘the_post’, ‘wpforce_featured’);