回答
你可以试试这个插件https://wordpress.org/plugins/category-posts/
如果你喜欢自己的代码。这里是:
您可以使用方法get_posts获取接受单个数组参数或字符串参数的结果,如下所示。
<?php $posts = get_posts('post_type=post&category=4');
$count = count($posts);
echo $count;
?>
解释为上述代码: 它获取文章表wp_posts
其post_type
是post
并且属于category
ID 4
导致。然后我们使用php函数count
获取总记录。
如果您只需要发布的帖子另外添加post_status=publish
。
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query(array('category_name' => 'announcements', 'posts_per_page' => 10));
$posts = get_posts('post_type=post&category_name=announcements');
$count = count($posts);
// The Loop
if ($the_query->have_posts()) {
$string .= "<p>Total:<strong>{$count}</strong></p>";
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ($the_query->have_posts()) {
$the_query->the_post();
if (has_post_thumbnail()) {
$string .= '<li>';
$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array(50, 50)) . get_the_title() .'</a></li>';
} else {
// if no featured image is found
$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');
通过使用上述功能,可以产生所得到的输出。 (仅供参考Show recent posts by category
只需访问Appearance » Widgets
菜单,然后添加一个text widget
您sidebar
,接着在文本窗口小部件添加[categoryposts]
短代码,并保存它。
喜@gvgvgvijayan'<? php $ posts = get_posts('post_type = post&category = 4'); $ count = count($ posts); echo $ count; ?>'如何显示当前类别中的帖子数量,当前类别ID ,而不是特定的类别ID? –
@ArianeMartinsGomesDoRego检查此https://wordpress.stackexchange.com/questions/247859/how-to-get-category-id-当前帖子 – gvgvgvijayan
感谢朋友的回复:-)我昨天发现了一些东西:'<?php $ category = get_the_category(); echo'('。$ category [0] - > category_count。'posts)'; ?>'谢谢你:-) –
- 1. 如何在子类别页面中显示Wordpress类别?
- 2. 在Wordpress中显示类别的帖子?
- 3. 在wordpress中只显示一个类别的显示类别描述
- 4. 如何根据WordPress的类别显示多个单页模板?
- 5. 在Wordpress中显示链接到类别?
- 6. 在WordPress循环中显示类别
- 7. 如何在wordpress中只显示每个类别的两篇文章?
- 8. 如何在wordpress中的单一类别中显示随机帖子
- 9. WordPress的 - 如何仅显示子类别
- 10. 在Wordpress中显示帖子的类别和子类别列表
- 11. WordPress的 - 在类别归档中使用wp_query - 如何显示适当的类别?
- 12. Wordpress显示类别的子类别
- 13. 显示WordPress的类别
- 14. 显示子类别单页面wordpress
- 15. 在wordpress中的页面上显示多个类别的帖子
- 16. 在WordPress中显示相关文章的类别
- 17. wordpress在父类别中显示子类别
- 18. 我想在wordpress中显示类别和子类别列表?
- 19. 在google结果中显示类别wordpress,如何?
- 20. 如何在wordpress中显示所有类别?
- 21. 如何在两列中显示wordpress类别?
- 22. 中显示多类别单个条目
- 23. 如何在prestashop中的父类别中显示子类别?
- 24. 显示类别下的一个WordPress的文章在页面
- 25. 如何根据页面中的类别显示WordPress的帖子
- 26. WordPress的:如果发布是在类别显示导航菜单
- 27. 在wordpress中,如何显示上一个和下一个类别名称?
- 28. WordPress的全文显示
- 29. 如何只显示一个类别的子类别中Rails_Admin
- 30. 显示下拉菜单中剩余的wordpress类别
哪里代码你的问题? – mickmackusa