按子类别显示 wordpress 帖子 [英] display wordpress posts by sub category
本文介绍了按子类别显示 wordpress 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 wordpress 新手,我使用的是 wordpress 4.5 版,我喜欢按子类别显示帖子,请任何人帮助我如何做到这一点.这就是我想要的
I am new in wordpress, I am using wordpress Version 4.5, I like to display posts by subcategories, anyone please help how can I do this. This is what I want
父母类别名称
子类别 1
Post 1
Post 2
子类别 2
Post 3
Post 4
子类别 3
Post 5
Post 6
...
提前致谢
推荐答案
如果我没猜错你需要这个,你需要双循环来获取子类别下的帖子
If I am not wrong you need this, you need double loop to fetch the posts under subcategories
这是您获取当前页面类别的方式
this is how you get current page category
<?php
$categories = get_the_category();
$catID = $categories[0]->cat_ID;
?>
然后通过使用上面的 catID 来做到这一点
and then do this by using above catID
<?php
$subcats = get_categories('child_of=' . $catID);
foreach($subcats as $subcat) {
echo '<h3>' . $subcat->cat_name . '</h3>';
echo '<ul>';
$subcat_posts = get_posts('cat=' . $subcat->cat_ID);
foreach($subcat_posts as $subcat_post) {
$postID = $subcat_post->ID;
echo '<li>';
echo '<a href="' . get_permalink($postID) . '">';
echo get_the_title($postID);
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
这篇关于按子类别显示 wordpress 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文