WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Latest Article in One Section...2 Previous Articles in another section of page? (4 posts)

  1. robertallen
    Member
    Posted 1 year ago #

    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.

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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();
    ?>
  3. robertallen
    Member
    Posted 1 year ago #

    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.

  4. robertallen
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic