WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display recent posts of parent category (7 posts)

  1. Misterpayne112
    Member
    Posted 5 years ago #

    i already have query code to display the last 5 posts of the same category on a single page post. i am looking for a query or code to display the last 5 posts of the parent category on the same page. is there anyone who can help with that? or can it simply not be done?

  2. MichaelH
    Member
    Posted 5 years ago #

    <?php
    $mycat=9; // use your category here
    $categories = get_categories('include='.$mycat);
    echo 'this is the parent ' .$categories[0]->category_parent
    ?>
  3. Misterpayne112
    Member
    Posted 5 years ago #

    the only thing that code says is... "this is the parent" it says that no matter the post, no matter the category.

    i use this code below for the current category on a sing post page:

    <?php
    if (is_single( )) {
    $post_ID = $wp_query->posts[0]->ID;
    $all_cats_of_post = get_the_category($post_ID);
    $first_cat_ID = $all_cats_of_post[0]->cat_ID;
    $first_cat_name = $all_cats_of_post[0]->cat_name;
    ?>
    <div id="category-posts-<?php echo $first_cat_ID; ?>" class="widget widget_recent_entries">
    <div class="widget-title"><b>Last 5 posts in <?php echo $first_cat_name; ?>:</b></div>
    <div class="widget-content">
    <table width=450><tr><td width=5%></td><td>
    <?php global $post; $cat_posts = get_posts('numberposts=5&category='.$first_cat_ID);
    foreach($cat_posts as $post) : ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><br />
    <?php endforeach; ?>
    </div>
    </div>
    <?php } ?>
    </td></tr></table>

    it displays the category name and links automatically, and i was looking for something similar for the parent category. for example if it is a post in my 'wrestling news' category, i want to show the last 5 post in that category (like i already do) and the last 5 post overall in the parent 'wrestling' category.

    is there a query or other way of doing that?

  4. MichaelH
    Member
    Posted 5 years ago #

    My code was just an example to show you how to get the parent of a given category. So now that you've presented your code we can use something a bit different:

    In your case you need another get_posts loop to retrieve the posts in the parent category:

    So after this line:

    $first_cat_name = $all_cats_of_post[0]->cat_name;

    add this:

    $first_cat_parent = $all_cats_of_post[0]->category_parent

    Then use the variable $first_cat_parent in your loop--for example:

    <?php
    if {$first_cat_parent) {
    $parent_posts = get_posts('numberposts=5&category='.$first_cat_parent);
    foreach($parent_posts as $post) : ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><br />
    <?php endforeach;
    }  // if $first_cat_parent
    ?>
  5. Misterpayne112
    Member
    Posted 5 years ago #

    ok that is great! just what i wanted! i have the links set up and they are working. my only problem (small problem) that i have left is getting the name to show up.

    i am using:

    <div class="widget-title"><b>Last 5 posts in <?php echo $first_cat_parent; ?>:</b></div>

    that is showing the parent category number (i.e. 49, 27, etc.) but I want it to show the name instead. i tried:

    <div class="widget-title"><b>Last 5 posts in <?php echo $first_cat_parent_name; ?>:</b></div>

    but that didn't work.

  6. MichaelH
    Member
    Posted 5 years ago #

  7. taeke
    Member
    Posted 4 years ago #

    @MichaelH, I'm using your excellent piece of code above. One question though, how would I go about to exclude the current post from the list of posts under the same category?

Topic Closed

This topic has been closed to new replies.

About this Topic