最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Display children of the category a post is assigned to

programmeradmin3浏览0评论

The following code works to display all child categories of a parent (when viewing a parent category), and all siblings of a child category (when viewing a child category) within sidebar.php. The same sidebar is pulled into both posts and pages.

I'd like to take it a step further so it also works when viewing a post, instead of only categories and child categories. On a post, I'd like all child categories of the category assigned to a post to display. After spending countless hours, I'm at a loss. Can anyone help me with this?

    <ul>                
            <?php if (is_category( )) {                             
                  $cat = get_query_var('cat');
                  $thiscat = get_category ($cat);
                  $parent = $thiscat->parent;
                  $img = get_option('z_taxonomy_image' . $thiscat->term_id);

                  if ($parent != '') {

                        wp_list_categories( array(
                        'child_of' => $parent,
                        'exclude' => $cat,
                        'title_li' => 0
                    ) );
                    }

                  else {

                        wp_list_categories( array(
                        'child_of' => $cat,
                        'title_li' => 0
                    ) );
                  }

            } 
            ?>

            </ul>

The following code works to display all child categories of a parent (when viewing a parent category), and all siblings of a child category (when viewing a child category) within sidebar.php. The same sidebar is pulled into both posts and pages.

I'd like to take it a step further so it also works when viewing a post, instead of only categories and child categories. On a post, I'd like all child categories of the category assigned to a post to display. After spending countless hours, I'm at a loss. Can anyone help me with this?

    <ul>                
            <?php if (is_category( )) {                             
                  $cat = get_query_var('cat');
                  $thiscat = get_category ($cat);
                  $parent = $thiscat->parent;
                  $img = get_option('z_taxonomy_image' . $thiscat->term_id);

                  if ($parent != '') {

                        wp_list_categories( array(
                        'child_of' => $parent,
                        'exclude' => $cat,
                        'title_li' => 0
                    ) );
                    }

                  else {

                        wp_list_categories( array(
                        'child_of' => $cat,
                        'title_li' => 0
                    ) );
                  }

            } 
            ?>

            </ul>
Share Improve this question asked Dec 22, 2016 at 23:17 jlohse22jlohse22 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

get the category or categories of the single post with get_the_category() and loop through them, calling the child category lists if available.

<ul>
<?php 
if( is_single() ) {
    $post_cats = get_the_category();
        foreach( $post_cats as $post_cat ) {
            wp_list_categories( array(
                'child_of' => $post_cat->term_id,
                'title_li' => 0,
                'show_option_none' => ''
            ) );
    }
}
?>
</ul>
发布评论

评论列表(0)

  1. 暂无评论