admin管理员组文章数量:1305728
I know I can use wp_list_categories()
to list categories, but how can I achieve to list also the posts under the respective categories, below an example what I want to achieve:
- category1
- -- post under cat1
- -- post under cat1
- -- post under cat1
- category2
- -- post under cat2
- -- post under cat2
- -- post under cat2
I know I can use wp_list_categories()
to list categories, but how can I achieve to list also the posts under the respective categories, below an example what I want to achieve:
- category1
- -- post under cat1
- -- post under cat1
- -- post under cat1
- category2
- -- post under cat2
- -- post under cat2
- -- post under cat2
1 Answer
Reset to default 0Finally I resolved the solution this way:
$hg_categories = get_categories();
foreach($hg_categories as $hg_category) {
echo '<h3>' . $hg_category -> name . '</h3>';
$hg_posts = get_posts(array('numberposts' => -1, 'category' => $hg_category -> cat_ID));
foreach ($hg_posts as $hg_post) {
if ($hg_post -> ID == get_the_ID()) {
echo '<li class = "current-post"><a href = "';
} else {
echo '<li><a href = "';
}
echo get_permalink ($hg_post -> ID);
echo '">';
echo $hg_post -> post_title;
echo '</a></li>';
}
}
if anyone have a better solution, please let me know, thank you.
本文标签: List categories with posts
版权声明:本文标题:List categories with posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741810014a2398736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_categories()
so you don't have to fight with the formatting. developer.wordpress/reference/functions/get_categories – Tony Djukic Commented Jan 23, 2021 at 15:02