WordPress.org

Forums

Show second newest post (4 posts)

  1. aled2305
    Member
    Posted 1 year ago #

    Hello, I currently use

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <?php
        // Fetch most recent posts, limit to 1
    		$latest_posts = get_posts( array(
    			'posts_per_page' => 1,
    		) );
        // get first and only item in array
    		$latest_post  = $latest_posts[0];
    	?>
    
    	<h3><a href="<?php echo get_permalink( $latest_post->ID ); ?>"><?php echo $latest_post->post_title; ?></a></h3>
    	<?php if ( has_excerpt( $latest_post ) ) : ?>
    	<p><?php echo $latest_post->post_excerpt; ?></p>
    	<p>
    	  <?php else : ?>
    	  <?php echo strip_shortcodes(wp_trim_words( $latest_post->post_content, 50 )); // '250' is number of words ?>
    
    	  <?php endif; ?>
    
    	  </p>
    	<p><a href="<?php echo get_permalink( $latest_post->ID ); ?>"> Read More...</a></p>
            </div>

    to show my most resent post, but what need's to be changed to show the second most resent post?

    Thanks

    Aled

  2. Dave Naylor
    Member
    Posted 1 year ago #

    Use WP_Query instead. Along these lines:

    $the_query = new WP_Query( 'offset=1' );
    
    [---- Your Loop   -----]
    
    wp_reset_postdata();
  3. aled2305
    Member
    Posted 1 year ago #

    Sorry @WPRanger but I'm new to wp coding. HTML and CSS i'm fine with but I haven't a clue what you mean. The code I used I found online, therefor could you please explain a bit more.

    Thanks

    Ale

  4. Dave Naylor
    Member
    Posted 1 year ago #

    If you look on the WP_Query codex page, there are quite a few examples of loops for you to look at.

Topic Closed

This topic has been closed to new replies.

About this Topic