The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Parent Category and its Children - How To (1 post)

  1. heronakamura
    Posted 5 years ago #


    I found the code below here somewhere, I do apologise as I can't remember who it was that provided this.

    My site uses category, a child category and a grandchild. On the category page, I had used the code below so it shows the child/grand child category of a given category/child category.

    But my problem is this, on the grandchild category (which has no subcategory of its own) it shows the same grandchild and the rest of the grandchildren category of the child category. Hope this makes sense.

    How can I make the "if rule" work. i.e if its showing the grandchild with no grand/grandchild category do not show anything.

    if(is_category()) {
        $breakpoint = 0;
        $subcategories = get_terms('category', 'parent='.get_query_var('cat'));
        $thiscat = get_term(get_query_var('cat'),'category');
        while(empty($subcategories)) {
            $subcategories = get_terms('category', 'parent='.$thiscat->parent.'');
            if($breakpoint > 10) break; // Avoid infinite loop (you never know)
        foreach($subcategories as $subcat) {
            if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
            $items .= '
            <li class="cat-item cat-item-'.$subcat->term_id.$current.'">
                <a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.' </a>
        echo "<ul>$items</ul>";
            <h2 class="dotted">Latest Articles on <?php
                        printf( __( ' %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.