• Hi there,

    I have about 8 categories in my blog and I want display the latest 6 post on the homepage, but I want each post come from different category, what I have is this:

    <?php
    $my_query = new WP_Query(‘showposts=6’);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>

    <h3 id=”post-<?php the_ID(); ?>”>” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></h3>

    <?php endwhile; ?>

    but the above code will display multiple posts from the same category, can someone please help me with this?

    Thanks a lot!

Viewing 15 replies - 1 through 15 (of 16 total)
  • Might try this:

    <?php
    //for each category, show 1 post
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    Thread Starter iewei

    (@iewei)

    Hi Michael

    Thank you very much

    I forgot to mention that I have 8 parent categories, each of them have few child categories, and I only want show the latest 6 posts which come from different parent category, can you help with this?

    Thanks!

    Didn’t test this but try:

    <?php
    //for each category that has no parent, show 1 post
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        if ($category->parent == 0) {
          $args=array(
            'showposts' => 1,
            'category__in' => array($category->term_id),
            'caller_get_posts'=>1
          );
          $posts=get_posts($args);
            if ($posts) {
              echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
              foreach($posts as $post) {
                setup_postdata($post); ?>
                <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                <?php
              } // foreach($posts
            } // if ($posts
          } //if parent == 0
        } // foreach($categories
    ?>

    Thread Starter iewei

    (@iewei)

    Hi Michael, this doesn’t show the posts under child categories, it only display the posts directly under the parent category

    Well I’m not sure what you are wanting then.

    Please review Function_Reference/get_categories and change your arguments accordingly.

    Pete

    (@perthmetro)

    “I only want show the latest 6 posts which come from different parent category…”

    “it only display the posts directly under the parent category ..”

    Sounds like he did as you asked?

    Thread Starter iewei

    (@iewei)

    Hi Michael,

    Sorry about the confusion, my English is not good enough.

    for example I have the following 8 categories and each parent category have two sub categories:

    Parent Category 1
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 2
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 3
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 4
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 5
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 6
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 7
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    Parent Category 8
    — Sub Category 1 (10 posts)
    — Sub Category 2 (10 posts)

    I have 10 posts under each sub category, now on the index page, I want show the latest 6 posts, which each of the post has to come from different parent category(the post is actually belong to the sub category), does this make sense?

    Okay I see what you want but not sure how to do that…maybe someone else can present a solution.

    Thread Starter iewei

    (@iewei)

    can anybody please help me?

    Pete

    (@perthmetro)

    Why not just have 6 separate pieces of code with each one only including the relevant 8 categories with the latest 6 posts?

    @perthmetro,
    I think the problem with that approach is that you can’t know ahead of time which 6 of the 8 will have the latest posts. I believe the code I posted earlier will do the job.

    if you take the last code that @michaelh posted, and change:

    'category__in' => array($category->term_id),

    into:

    'cat' => $category->term_id,

    then hopefully it won’t just use only the parent category, but also allow the child categories.
    (https://codex.www.ads-software.com/Template_Tags/query_posts#Category_Parameters)
    (it obviously will output 8 posts)

    @vtxyzzy:
    your code looks like it is going to do the job –
    i just wonder, would the full query of all posts not use a lot of resources?

    I thought about that, but couldn’t come up with a good alternative. You could limit the number, but then you would stand a chance of not getting 6 recent posts.

    You could use multiple loops each retrieving 1 post and exclude that category from remaining loops. but would six queries for 1 post each be better than one? I don’t know how to decide that.

    Pete

    (@perthmetro)

    I’m not sure i’m gettin’ this ?????

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘How to display the latest 6 posts (one post from each category only)’ is closed to new replies.