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.
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘To output parent category name with link’ is closed to new replies.