Display category description ONLY when parent category MORE THAN 1 above (2 posts)

  1. perthmetro
    Posted 2 years ago #

    OK, this is going to be a challenge to describe but i'll try...

    In my archive.php I have a list of posts, I want each post to be able to display the description of the child category in which it sits... no problems, I can do that with <?php $category = get_the_category(); echo $category[0]->category_description; ?>. However, I only want that description displayed if the post is being listed under a category only when that parent category is more than 1 above it...

    Make sense? Let me try to explain it another way:

    cat: XYZ
    cat/sub-cat: XYZ/abc
    cat/sub-cat/post: XYZ/abc/post

    abc archive.php = don't show cat description of post 123
    XYZ archive.php = SHOW cat description of post 123

    Thanks heaps on this one!

  2. greendemiurge
    Posted 2 years ago #

    It looks like getting the depth of elements is tricky and there may still not be a built-in function to do it.

    Here is a site that defines a function you could try:

    Basically, what you would do (provided this code still works) is put the function in functions.php, then, within your archive.php template, you would call the function in two places.

    First you would call it outside of the loop setting a variable to capture the depth of the category
    <?php $categoryDepth = get_depth(); ?>

    Then you would set it inside the loop to locally capture the depth of the article:
    <?php $articleDepth get_depth(); ?>

    Then you would put your category description inside a conditional (also inside the loop):

    if (($categoryDepth - $articleDepth) > 1)
    $category = get_the_category(); echo $category[0]->category_description;

    If the code snippet on the linked page still works as advertized you would be in business.

Topic Closed

This topic has been closed to new replies.

About this Topic