1

I have 3 levels of categories:

Cat 1
- Cat 2
- Cat 3
-- Cat 4

I want to add Parent category name after page title. For example:

<h1>Cat 4</h1>
<p>From Cat3 in Cat 1</p>

But only for third level. Is it possible and what is the better way to do this correct?

I have fond a way to get Cat3, but I still bon't know, how to get previous category name and how to display the message for only third level.

$parent = get_queried_object()->parent;
if( $term = get_term_by( 'id', $parent, 'product_cat' ) ){
    echo '<span class="sub-cat">' . $term->name . '</span>';
}

Thanks!

1
  • Yes, it's possible. How to accomplish it depends on your theme. You would first want to create a child theme, then copy whichever file applies - could be category.php or archive.php - and then add your code. Once you verify you're in the right file, build out the logic that checks the current category to see if it has a parent and grandparent, but not a great-grandparent, and if so outputs that paragraph.
    – WebElaine
    Dec 5, 2019 at 21:56

1 Answer 1

0

You could have a category description. For example the Cat 4 will have the category description as : From Cat3 in Cat 1.

You add the category description when you edit the category inside of WP > Posts > Categories

Then to display the category description use this PHP code inside of your archive.php theme file:

<?php echo category_description(); ?>

Your Answer

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

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