WordPress循环-如何计算项目 [英] Wordpress loop - how to count items

查看:80
本文介绍了WordPress循环-如何计算项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在Wordpress循环代码中获取大量项目:

Is there a way to get a number of items within Wordpress loop code:

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

此循环列出了帖子. 我需要根据某些类的总数将某些类添加到前3个.

This loop lists the posts. I need to add certain classes to first 3 depending on the total number of them.

推荐答案

您可以使用 $WP_Query的>属性,如下所示:

You can use the post_count property of $WP_Query, like so:

$wp_query->post_count

请注意与found_posts的区别,found_posts计算尽管匹配查询但未显示(例如,用于分页)的帖子.您可能要根据自己的具体情况使用其中一个.

Be aware of the difference with found_posts, which counts the posts which, though matching the query, are not being displayed (e.g. for pagination). You might want to use one or the other depending on your particular situation.

这篇关于WordPress循环-如何计算项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆