WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Latest Article in One Section…2 Previous Articles in another section of page?

[Resolved] Latest Article in One Section…2 Previous Articles in another section of page?

  • Hi all,

    I need to be able to have a section called “Latest Article” which would display the latest post in a blog.

    Then in another section on the web page I need to display the 2 previous articles that were published before the most recent in a blog.

    So
    Latest Article

    Past Articles (Displays 2)

    Is this possible? I’ve looked all around and I can’t seem to find an answer.

    Any help would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael

    @alchymyth

    Forum Moderator

    can all be done with either get_posts() or WP_Query() and the ‘posts_per_page’ and ‘offset’ parameters;

    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Class_Reference/WP_Query
    http://codex.wordpress.org/The_Loop

    example:

    <?php
    $latest = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 1));
    if( $latest->have_posts() ) : while( $latest->have_posts() ) : $latest->the_post();
    /*your php output here*/
    endwhile; endif; wp_reset_postdata();
    ?>

    and

    <?php
    $past = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 2, 'offset' => 1));
    if( $past->have_posts() ) : while( $past->have_posts() ) : $past->the_post();
    /*your php output here*/
    endwhile; endif; wp_reset_postdata();
    ?>

    Oh I see, so the offset is what makes it go one down then right?

    Thank you so much for this! I looked through everything, I must’ve missed how to combine it all.

    robertallen

    @robertallen

    Oops, forgot to mark as resolve. Thank you again!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Latest Article in One Section…2 Previous Articles in another section of page?’ is closed to new replies.
Skip to toolbar