Support » Developing with WordPress » To output parent category name with link

  • Hi there,

    I’m trying to write code to output parent category name with url link.
    For example, post “I like sweet things” belongs to category “gummy” that has parent category “lollipop”. I want to be able to output “lollipop” with url going to lollipop (not direct category gummy but its parent).

    My code is below and it seems very close, however this code outputs “gummy” for category name but with url of “lollipop”. The URL is correct but category name should output parents name (lollipop). By the way, when post category doesn’t have parent, this outputs category name and url both correctly.

    function mo_entry_catname() {
    		$cat_now = get_the_category();
    		$cat_now = $cat_now[0];
    		$parent_id = $cat_now->category_parent;
    		$cat_name = $cat_now->name;
    		$catname = '<span class="category"><a href="' . get_category_link( $parent_id ) . '">' . $cat_name . '</a></span>';
    		return $catname;
    }

    It would be great if there is anyone could take a look.

    • This topic was modified 2 years, 1 month ago by  palmtree.
Viewing 7 replies - 1 through 7 (of 7 total)
  • for example, change this line:

    		$cat_name = $cat_now->name;
    

    to:

    		$parent_cat = get_category( $parent_id );
    		$cat_name = $parent_cat->name;
    

    https://codex.wordpress.org/Function_Reference/get_category

    Thank you Michael,
    Your solution is getting close… because now I can see “lollipop” with URL link for the post “I like sweet things”. But for other post which doesn’t have parent category lost category output.

    For example when the post “I like sweet things” belongs to “gummy” category that has no parent, there will be no output (neither gummy nor its url pointing to gummy) by using the code above so the <span class=”category”> becomes empty.

    I think the code above only works for the post that belongs to category which has parent…

    For this (when cat doesn’t have parent cat), the previous code was fine. Only what’s missing in previous code was not being able to output “parent” name (when cat has parent) although the URL link was of the parent.

    • This reply was modified 2 years, 1 month ago by  palmtree.
    • This reply was modified 2 years, 1 month ago by  palmtree.
    • This reply was modified 2 years, 1 month ago by  palmtree.
    Moderator bcworkz

    (@bcworkz)

    You need a conditional structure to account for there not being a parent category. Top level categories with no parent have $cat_now->category_parent = 0, so use a conditional like this:

    if ( 0 == $cat_now->category_parent ) {
      //your initial code
    } else {
      // Michael's code
    }

    Thanks bcworkz. Conditional structure made sense to me but the result was the same as what my initial code does.

    Initial code was fine except for the print name (name was child cat but url was parent cat. The print name and url should be both parent) and it works fine with post under the cat that doesn’t have parent.

    I wonder how this can be done…

    Moderator bcworkz

    (@bcworkz)

    Ah, I see. I failed to recognize what your initial code was doing. I assumed instead of reading. Sorry for any confusion.

    You still need the conditional structure, the code within needs to be tweaked though. I think this will do what you want:

    $cat_now = get_the_category();
    $cat_now = $cat_now[0];
    if ( 0 == $cat_now->category_parent ) {
       $catname = '<span class="category"><a href="' . get_category_link( $cat_now->term_id ) . '">' . $cat_now->name . '</a></span>';
    } else {
       $parent_id = $cat_now->category_parent;
       $parent_cat = get_category( $parent_id );
       $catname = '<span class="category"><a href="' . get_category_link( $parent_id ) . '">' . $parent_cat->name . '</a></span>';
    }
    return $catname;

    Completely replace what your function does with this.

    Your code solved everything!!
    Thank you so much:)

    Moderator bcworkz

    (@bcworkz)

    Always happy to help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘To output parent category name with link’ is closed to new replies.