WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] post meta data: display the child category from one parent only (5 posts)

  1. grn
    Member
    Posted 5 years ago #

    If a post has been filed under multiple categories, the default is to list each category at the end of each entry.
    However I want to display only one category at the end of an entry. To give an example, one of the Parent categories is "Author", and its child categories are "author-1", "author-2", "author-3" etc. I want only the child category "author-x" to be displayed.
    There are other categories that I also have the post filed under(Book Title, Publisher etc), but I want to omit these for now. Any ideas?
    I have spent a long time googling and cannot find any answers.
    Thank you for any replies.

  2. MichaelH
    Member
    Posted 5 years ago #

    To give an example, one of the Parent categories is "Author", and its child categories are "author-1", "author-2", "author-3" etc. I want only the child category "author-x" to be displayed.

    Well author-x isn't your choice. Which of those three do you want?

    Also, is it always the Author category that you are wanting this? Or are there other Parent/child categories that must be dealt with?

  3. grn
    Member
    Posted 5 years ago #

    Thank you for the reply.
    Yes I only want to list the author. The other parent/child categories will be dealt with elsewhere..
    The list of child categories mentioned above("author-1" etc.) was said as an example. They are not the actual names! Maybe I didn't explain it very well...

    Each post will be filed under an author. Its parent category is called "Author". So I want to have the author name(child of "Author") displayed on it's own, excluding the other parent and child categories associated with the post("Book Title", "Publisher" etc).
    By default the_category() lists all categories and I don't want this.
    Any ideas or useful links would be appreciated.

  4. MichaelH
    Member
    Posted 5 years ago #

    So you want only the Category displayed if it is a child of "Author"?

    <?php
    //get categories for this post and only display if the category parent is Author
    $author_cat=get_cat_ID('Author');
    $categories=get_the_category();
      foreach($categories as $category) {
        if ($category->parent == $author_cat) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        }
      }
    ?>
  5. grn
    Member
    Posted 5 years ago #

    Wow thank you so much for this! The title text also. You have made my day :)

Topic Closed

This topic has been closed to new replies.

About this Topic