Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

<?php
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?

share|improve this question

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.

share|improve this answer
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;
     break;

  }

}

share|improve this answer
1  
Would you explain what your code does please? –  s_ha_dum Jan 18 '13 at 22:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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