• I have a lot of nested categories and need to be able to display navigation links on the parent category pages to the child/sub categories, and a link back to the parent page from the child/sub category pages.

    I would also like to be able to exclude parent and child links from select categories all together.

    It would also be nice to include links to other child categories of the same parent. For instance I have Category A which has child categories A1 and A2. I would like to have a link on the A1 category page to A2 and a link to A1 on the A2 page. This however is of lowest priority.

    Currently I am using this:

    <!-- begin -->
    
    <?php
    
            //for this category on an archive page, get the ID
            $thisID = get_query_var('cat');
    
            $get_children_cats = array(
                'child_of' => $thisID //get children of this parent using the thisID variable from earlier
            );
    
    //firstly, load data for your child category
    $child = get_category($thisID);
    
    //from your child category, grab parent ID
    $parent = $child->parent;
    
    //load object for parent category
    $parent_name = get_category($parent);
    
    //grab a category name
    $parent_name = $parent_name->name;
    
                echo '<strong><a href=" ' . get_category_link( $parent ) . ' ">' . $parent_name . '</a></strong><p>';
    
            $child_cats = get_categories( $get_children_cats );//get children of this parent category
    
            foreach( $child_cats as $child_cat ){
                //for each child category, get the ID
                $childID = $child_cat->cat_ID;
    
                echo '<strong> - <a href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a></strong>';
            } //end of categories logic 
    ?><p>
    <!-- end -->
    

    This code does display the Parent and Child Category links but I would like to be able to include IF statements so I can have info like “Back To: <parent page link>” included ONLY on child pages and to be able to have text symbols appear between the child category links but NOT before or after the links (or not at all if there is only one child).

    Unfortunately I don’t have enough programming or PHP to be successful with IF statements and would appreciate some advice.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You can tell if a category term is a child term or not from the term object’s parent property. Any non-zero parent value indicates a child term. In your code, the parent value is assigned to $parent. In PHP, a zero value evaluates as false, and any other value is true, so $parent by itself is adequate for an if() conditional.

    if ( $parent ) {
       // do things only for child terms
    }

    You can get other immediate siblings of a term (but not descendants from other generations) by using the “parent” argument for get_categories(). Use the “exclude” argument to prevent the current term from being returned with its siblings.

    $siblings = get_categories( array(
       'parent' => $parent,
       'exclude' => $child,
    ));

    You can then do a foreach loop with $siblings to output links just as you’ve done with $child_cats.

    Using loops to output lists is a powerful coding technique, but we’re always left with having to suppress any terminal separator text after the final list item. The foreach() loop structure does not let us determine when the last item is encountered. We could instead use a basic for($start:$end:$increment) type loop. Then the end condition is known and we can conditionally suppress the terminal separator text. I really like the foreach structure though, and the for() structure with conditional separator text is kind of messy.

    We can use foreach() and still suppress terminal separator text by instead of echoing out links, we collect them into an array and let the implode() function insert the separator text. It will then figure out how to suppress terminal separator text internally. We don’t have to address the issue at all.

    $list = array(); // initialize an empty array
    foreach ( $cats as $cat ) {
       $list[] = $cat->name;  // add elements to the array
    };
    echo implode('|', $list );

    The output might be something like “foo|bar|sna|foo” The array does not have to be exclusively built by a foreach structure. You could also add or remove elements before imploding. Whatever array you end up with will have all elements listed, each separated by the first implode() argument, in my example a pipe ‘|’.

Viewing 1 replies (of 1 total)
  • The topic ‘Display Parent Category Link on Child Category Page’ is closed to new replies.