WordPress.org

Ready to get started?Download WordPress

Forums

Can I echo the direct parent of a child category? (1 post)

  1. TheRG
    Member
    Posted 1 year ago #

    Hi all!

    First I had a category (say 'X') with only one level of child categories beneath it ('X1', X2', 'X3', ...) , so it was easy to display a link to this child categorie by means of this snippet:

    <?php
    $sep = '';
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(10, $childcat)) {
    echo $sep.'<a class="white" href="'.get_category_link($childcat->cat_ID).'">',$childcat->cat_name,'</a>';
    $sep = ', ';
    }}
    ?>

    But now I have to add another level, so the hierarchy becomes: X -> X1 -> X1a, X1b, X1c, .....

    When I display a post from categoroy X1c, how do I tweak the snippet in such a way that I can display X1 underneath it as a link. I have been trying for hours now, but without success...

    All help would be much appreciated!

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic