0

I want the user to tick the child cat only, but I'd like to be able to display the parent cat based on the child cat. This is within the loop.

2 Answers 2

1

There's a function called get_category_parents that will return a list of parents based on the ID. You can then loop through this list to display the parents.

5
  • I tried <?php echo get_category_parents( $cat, true, ' ' ); ?> but it didn't work.
    – Pete
    Sep 22, 2019 at 12:48
  • Maybe $cat is a term object? Try $cat->term_id. Sep 22, 2019 at 12:54
  • Yeah, that example in the codex doesn't work. You cannot echo a list. You'll have to loop through it.
    – cjbj
    Sep 22, 2019 at 13:02
  • General tip if echoing doesn't work. Use var_dump to see the structure of the variable you are trying to print. That will tell you how to dissect it in order to echo it.
    – cjbj
    Sep 22, 2019 at 13:03
  • <?php echo get_category_parents( $cat->term_id, true, ' ' ); ?> didn't work either. I'm not very familiar with php.
    – Pete
    Sep 22, 2019 at 13:10
0

get_category_parents() is a function which returns the category's parent based on the cat ID.
So in this case to make it adaptable, we use get_queried_object_id() which will output the cat ID (number) of current object.

It's as simple as :

$parents = get_category_parents( get_queried_object_id());
if ( ! is_wp_error( $parents )) echo $parents;

It's good practice to implement the WP_Error fallback, mostly to avoid abritratry code execution.

A little PHP breakdown for those beginning:

  1. we declare the variable $parents
  2. $parents will equal everything between the "=" sign and next semi-colon ";"
  3. get_queried_object_id() returns the current object id (current category number)
  4. That number is piped in get_category_parents() -> i.e get_category_parents('26')
  5. Last function returns name category 26 parent's name -> say 'Master Category' for example
  6. Semi-colon ! So end of this 'loop', simple way to think of it :

$parents = get_category_parents(26) -> $parents = 'Master Category'

i'll quickly breeze through the last part, you're all smart cookies eager to figure it all out on your own ;)

  1. if $parents doesn't equal wp_error echo $parents

End of story !

4
  • unfortunately <?php $parents = get_category_parents( get_queried_object_id()); if ( ! is_wp_error( $parents )) echo $parents; ?> didn't display anything?
    – Pete
    Sep 23, 2019 at 10:26
  • can you provide the code of page you placed it in, what loop it's in? Sep 23, 2019 at 14:32
  • It's just in a normal single.php loop.
    – Pete
    Sep 23, 2019 at 15:12
  • the following will only work for single content page, otherwise i suggest using a hook in the functions file. In the file /content/parts/content-single.php of your theme folder, <?php the_category(',&nbsp; &nbsp;'); ?> replace by <?php $category = get_the_category(); $first_category = $category[0]; echo sprintf( '<a href="%s">%s</a>', get_category_link( $first_category ), $first_category->name );?> Sep 24, 2019 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.