且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按子类别显示 wordpress 帖子

更新时间:2022-11-19 23:41:35

如果我没猜错你需要这个,你需要双循环来获取子类别下的帖子

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>';
    } 
?>