0

I'm using the Category description, for the parent category, to display above the posts on the category.php page.

Parent Category page, showing desc above two posts

But, this desc doesn't show on any sub-categories of this parent category, and there is no desc being added to each sub-category (there are over 270 cats in total, so duplicating desc from parents to subs would be tedious at best!)

Is there way to show the Parent categories desc for the parent and all it's subcategories?

1 Answer 1

1

Approach number one

If you are hard-coding a theme and you are writing your own category.php template, the first thing you have to do is to know what category you are displaying. You can use get_queried_object() to do that, which will return the current queried object.

$category = get_queried_object();

In your case you are calling the category.php template so it will return the queried category object.

The category object will be an instance of the WP term class. And as you can see in the docs, it contains the parent attribute.

$parent_cat_id = $category->parent;

Then, you can get the parent category description as follows:

$description = category_description($parent_cat_id);

See the docs for category_description()

Of course you will also have to check if the current category has a parent or not. You can do that with a simple if statement:

if($category->parent > 0){
$parent_cat_id = $category->parent;
}

Please note that by default, the parent attribute is set to 0. That means that if your value of $category->parent is equal to 0, that category does not have a parent. It is a primary category.

Approach number two

If you want to use filters, then you could use the category_description hook, which filters the category description for display.

Insert the following add_filter() call into your functions.php or custom plugin:

function wpse_display_parent_category_description($description, $category_id){

    $category = get_category($category_id);
    
    // Return the parent category description if it's a child category
    if($category->parent > 0){
    $parent_cat_id = $category->parent;
    return category_description($parent_cat_id);
    }

    // Or just return the original description if it's a primary category
    return $description;
}

add_filter('category_description', 'wpse_display_parent_category_description', 10, 2);
10
  • Thanks for the quick response! I prefer the filter method, but it throws an error: Fatal error: Uncaught ArgumentCountError: Too few arguments to function display_parent_category_description(),
    – revive
    May 7, 2021 at 14:13
  • @revive Ups, sorry for that. Updated the answer. The thing is you must provide the accepted args parameter to the add_filter() function. In this case, the category_description hook accepts 2 arguments. Don't forget to mark it as accepted if it helped you. Have a nice day! May 7, 2021 at 14:31
  • Ah, I didn't even catch that #facepalm.. I made that change but it doesn't show the parent cat desc on the child cat.. I var_dump'd the $category->parent var and it's null.. on a child category, with a parent cat that does have a desc.. thoughts??
    – revive
    May 7, 2021 at 14:37
  • @revive In old WP versions the $category->parent does not exist. It was named $category->category_parent. Which WP version are you using? May 7, 2021 at 14:47
  • We're running 5.7.1
    – revive
    May 7, 2021 at 14:48

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.