Search code examples
phpwordpressblogsposts

Select a specific blog post just by its position in the ‘latest’ order


I want to have 3 rows of 3 posts on my home page. The entries will be the top 9 most recent, and need to be responsive.. ie: thumbnails resize smaller until they have to end up just one per row. I will only be displaying (approx) a 360×180 thumbnail and the title underneath the image (both linked to the actual post location).

The only way I can think to do this is with coding responsive container rows & columns (my theme is bootstrap based). This would be easy to do, if I could dynamically call the most recent post into the first cell, then call the 2nd most recent post into the 2nd cell, then the 3rd most recent post into the 3rd cell, etc.

Is there a way for php to know, lets say, which is the 6th most recent post? How would you call it?

Here is the coding I currently have on the home page. At the moment, I am using a loop to parse through the posts (Note: I am only including draft post while testing.) It looks great until you get an oversized image, or start to resize the window smaller. Then the images in the row start running into each other (not resizing), until they finally break out into a single row each.

<div class="container blog-posts margin-bottom-60">
  <div class="row">

    <?php $args = array( 'numberposts' => 9, 'order'=> 'ASC', 'orderby' => 'title', 'post_status' => array( 'publish', 'draft') );
        $postslist = get_posts( $args );
        foreach ($postslist as $post) :  setup_postdata($post); ?> 
            <div class="col-md-4 col-sm-4 col-xs-12" style="float: left; display:block">
                <div class="img-responsive" style="margin: 10px; "><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></div>
                <center><h3 class="entry-title" style="text-align: center;"><a class="entry-title-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><center>
            </div>
    <?php endforeach; ?>


    <br clear="all" />
    <br />

    <a class="blog-button" href="/aphrodisiacs-blog/">View More
    >></a>


  </div>
</div>

Instead, I would like to be able to set up a typical bootstrap grid, and just place one post into each cell as mentioned above.. starting with the most recent down to the 9th most recent.

Is this possible?

Thanks for any help.

Cheers, SunnyOz


Solution

  • To retrieve the 9 most recent posts, you just need to change your order and orderby query arguments.

    Instead of;

    $args = array( 'numberposts' => 9, 'order'=> 'ASC', 'orderby' => 'title', 'post_status' => array( 'publish', 'draft') );
    

    Use;

    $args = array( 'numberposts' => 9, 'order'=> 'DESC', 'orderby' => 'post_date', 'post_status' => array( 'publish', 'draft') );
    

    OR, just don't specify either, as they resolve to DESC and post_date by default. You can read up on WP Query arguments here.

    PS: to select the 6th most recent post, you can just keep track of a counter inside your foreach loop and add conditional code when counter = 6.