• Resolved jbro

    (@jbro)


    Hi, on the home page of a site I’m currently working on, I want to display the most recent post from each of several featured categories, restricted to 10 posts total but only one post from each of the categories.

    I have a home.php with a query_posts statement before the loop, which displays the featured categories properly; the problem is it goes more than one post deep into each category. Is there a way to get only the most recent post for each cat? Is query_posts best here or some other function?

    Here’s some code:

    <!-- ?php query_posts('cat=10,9,8,7,6,5,4,3,2,1&showposts=10'); ? -->
    
    <?php while (have_posts()) : the_post(); ?>
    
    -- display post using the_excerpt and the_thumbnail --
    
    <?php endwhile; ?>
    
    <?php wp_reset_query(); ?>

    Obviously, this displays posts from featured categories (great), but shows the 10 most recent posts from all of them (not what I need). Showposts=1 will only show one post total.

    I’ve also tried variations on cat=10&showposts=1,cat=9&showposts=1, etc, but that’s off-base.

    I don’t care about pagination in this instance, just the 10 featured categories.

    I’ve been all over the codex page for query_posts and unless I’ve missed something I can’t find a solution. I’ve tried several ways, including making the post categories sub-cats of a parent cat. The problem is that showposts restricts the total number of posts displayed.

    This is my first post to the forums, apologies if this has been covered and I didn’t find it, and for any snafus in my code display.

    Any thoughts? Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You can try looping through all categories and showing only one post. Something like this:

    <?php
      // array of category IDs
      $categories =  array(10,9,8,7,6,5,4,3,2,1);
    
      foreach ($categories as $cat) :
        $post = false;
        $post = get_posts('cat='.$cat.'&posts_per_page=1');
        if($post) :
          $post = $post[0];
          setup_postdata($post); ?>
        <!-- rest of normal loop -->
        <?php the_excerpt(); ?>
    
        <?php endif; ?>
      <?php endforeach; ?>

    Thread Starter jbro

    (@jbro)

    Thank you so much keesimeijer! That worked, with virtually no mods other than adding my loop and styles. I was stuck in a rut with query_posts, I never would have thought to use get_posts, nice to learn something new!

    I am so glad to find a working solution.

    However, can you tell me in which file to paste the codes?
    is it index.php?

    can you tell me the line where I need to paste this?

    I am using the Kippis 1.13.3 WordPress theme.

    For the ease of use………….
    I am providing you the coding of my index.php file

    <?php
    get_header();
    if (!$kippis_is_smartphone) get_sidebar('left');
    ?>
          <div id="content">
            <?php if (have_posts()) : ?>
            <?php kippis_content_nav('nav-above'); ?>
            <?php
            while (have_posts()) {
              the_post();
              get_template_part('content',get_post_format());
            }
            ?>
            <?php kippis_content_nav('nav-below'); ?>
            <?php else : ?>
            <header><h1><?php _e('Nothing Found','kippis'); ?></h1></header>
            <div id="post-0" class="article post no-results not-found">
              <p><?php _e('Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.','kippis'); ?></p>
              <?php get_search_form(); ?>
            </div><!-- article -->
          <?php endif; ?>
          <?php wp_link_pages(); ?>
          </div>
    <?php
    if ($kippis_is_smartphone) get_sidebar('left');
    get_sidebar('right');
    get_footer();

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. Or better still – use the pastebin. As it stands, your code may now have been permanently dmaged/corrupted by the forum’s parser.]

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    <?php
    get_header();
    if (!$kippis_is_smartphone) get_sidebar('left');
    ?>
          <div id="content">
          <?php
      // array of category IDs
      $categories =  array(10,9,8,7,6,5,4,3,2,1);
    
      foreach ($categories as $cat) :
        $post = false;
        $post = get_posts('cat='.$cat.'&posts_per_page=1');
        if($post) :
          $post = $post[0];
          setup_postdata($post);
           get_template_part('content',get_post_format());
          ?>
        <?php endif; ?>
      <?php endforeach; ?>
          </div>
    <?php
    if ($kippis_is_smartphone) get_sidebar('left');
    get_sidebar('right');
    get_footer();

    [email protected]

    (@danitheadventurebitecom)

    This is wonderful! I was able to get this to work in the loop but what I need to do with it is actually take this functionality and get it to work with an image slider.

    I am trying to do this with the Most Recent Posts Slider.

    I think I have the category array in properly but I am not sure where the foreach is supposed to go??

    Can anyone help me with this? I know its simple and I’ve been trying to get this to work for several years and have failed each time.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display most recent post from each of several categories on home page’ is closed to new replies.