WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Pete
    Member
    Posted 7 months 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
    Member
    Posted 7 months 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:
    http://www.devdevote.com/cms/wordpress-hacks/get_depth

    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):

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

Reply

You must log in to post.

About this Topic