WordPress:类别帖子计数

发布于 2025-01-23 14:34:00 字数 294 浏览 0 评论 0原文

我正在尝试找出一种获取特定类别中帖子数量的方法,然后在帖子本身上显示帖子编号。为了更好地解释,我正在在我的网站上创建一个投资组合部分,而不是将帖子ID显示为增量(因为我的ID没有顺序),我宁愿以某种方式计算我的投资组合类别中的帖子并显示增量。下图。

I am trying to figure out a way to get the number of posts in a specific category, then display the posts number on the post itself. To better explain, I'm creating a portfolio section on my site and rather than displaying the posts id as the increment (because my id's are not in order), I'd rather somehow count the posts in my portfolio category and display the increment. Example picture below.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2025-01-30 14:34:00

您将需要使用$ WP_QUERY并在循环中获取发布索引。请参阅下面的示例。 current_post将在循环中获取该帖子的索引。索引从0开始,因此我们将其添加到它,因此它将开始计数为1。

functions.php

function namespace_post_index() {
  global $wp_query;
  
  $postIndex = $wp_query->current_post + 1;

  echo $postIndex;

}

在循环中,使用我们上面创建的函数:

namespace_post_index();

You would need to use $wp_query and get the post index in the loop. See the below example. current_post will get the index of that post in the loop. The index starts at 0 so we add one to it so it will start counting at 1.

In functions.php:

function namespace_post_index() {
  global $wp_query;
  
  $postIndex = $wp_query->current_post + 1;

  echo $postIndex;

}

In the loop, use the function we created above:

namespace_post_index();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文