WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] post meta data: display the child category from one parent only

[Resolved] post meta data: display the child category from one parent only

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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?

    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.

    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> ';
        }
      }
    ?>

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] post meta data: display the child category from one parent only’ is closed to new replies.
Skip to toolbar