So basically, I have posts assigned to only subcategories and not to categories (this is so both of them get displayed in the premalink) and in the post page only the subcategory is displayed. I want to to display the parent category somewhere on the post page also.

I need something like this:

Sample Post belongs to SubcategoryName a part of Category
Buttercake belongs to Cakes a part of Desserts

I've tried this code:

foreach((get_the_category()) as $category) {
echo get_category_parents($cat, TRUE, ' &raquo; ');
echo $category->cat_name . ' ';

But I keep getting this error:

Catchable fatal error: Object of class WP_Error could not be converted to string in C:\wamp\www\wp-content\themes\yoko\content.php on line 22

Does anybody know how to achieve this?

2 Answers 2


in your foreach you assign get_the_category() to $category, but then inside get_category_parents(), you pass $cat instead of $category.

foreach((get_the_category( get_the_ID() )) as $category)
  if(cat_is_ancestor_of(get_cat_ID('x') , $category->cat_ID) )
     $category_link = get_category_link( $category->cat_ID );
     $category_name = get_cat_name( $category->cat_ID );
     $Finalcategory = $category;



  • 1
    Would you explain what your code does please?
    – s_ha_dum
    Commented Jan 18, 2013 at 22:41

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.