如何在标签页上显示帖子数?
在 wordpress 中(理想情况下,不使用插件)在标签页面上,我想显示用当前标签标记的帖子数量。
例子: 共有 8 条带有“棒球”标签的帖子 当您在棒球标签页上时,它会显示“有 8 个关于棒球的帖子”。
它应该动态地知道您在哪个标签页上,获取计数并打印它。我找到了几个静态输入标签名称或 ID 并返回计数的选项,但我尝试让它们动态工作的尝试没有成功。
这就是我正在处理的事情:
$taxonomy = "post_tag"; // can be category, post_tag, or custom taxonomy name
// Using Term Name
$term_name = single_cat_title;
$term = get_term_by('name', $term_name, $taxonomy);
// Fetch the count
echo $term->count;
任何帮助将不胜感激!
In wordpress (ideally, with out the use of a plug-in) on a tag page I would like to show the number of posts that are tagged with the current tag.
Example:
There are 8 posts with the tag "baseball"
when you are on the tag page for baseball it says, "There are 8 posts about baseball"
It should dynamically know which tag page your on, get the count and print it. I have found several options for statically entering the tag name or ID and returning the count but my attempts to get them to work dynamically have been unsucessful.
This is what I was working with:
$taxonomy = "post_tag"; // can be category, post_tag, or custom taxonomy name
// Using Term Name
$term_name = single_cat_title;
$term = get_term_by('name', $term_name, $taxonomy);
// Fetch the count
echo $term->count;
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以只打印 WP Query 对象的
found_posts
属性You can just print the
found_posts
property of the WP Query object打开模板中名为“tag.php”的文件,然后在其中输入所需的代码。
Open the file in your template named "tag.php" and enter the desired code in it.
这是 WP Recipes 的另一个可能的解决方案。在我在这里得到答复的第二天,他们很友善地发布了这篇文章。 :)
它有不同的应用程序,但仍然非常有用!
http://www.wprecipes.com/get-how-many-posts-are-returned-by -a-custom-loop?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Wprecipes+%28WpRecipes.com%3A+Daily+recipes+to+cook+with+WordPress%29
Here is another possible solution from WP Recipes. They were kind enough to post this the day after I got an answer here. :)
It has different application but still very useful!
http://www.wprecipes.com/get-how-many-posts-are-returned-by-a-custom-loop?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Wprecipes+%28WpRecipes.com%3A+Daily+recipes+to+cook+with+WordPress%29