• Resolved Konstabel

    (@konstabel)


    Dear fellows,

    I am trying to display only the last post’s title and contents on a page.
    I am able to extract the appropriate post, but do not know how to display the contents.

    This is the code I am using:

    <?php
      $args = array( 'numberposts' => '1' );
      $recent_posts = wp_get_recent_posts( $args );
      foreach( $recent_posts as $recent ){
        echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> ';
      }
    ?>

    I tried using get_content(); but it does not seem to help me.

    Can anyone please help?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Konstabel

    (@konstabel)

    My very next search got me a solution to post content but not the correct contents.

    I added the line echo $post->post_content;

    The complete code now reads:

    <?php
      $args = array( 'numberposts' => '1' );
      $recent_posts = wp_get_recent_posts( $args );
      foreach( $recent_posts as $recent ){
        echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>';
       echo $post->post_content;
      }
    ?>

    Would still like to hear your comments.

    Thread Starter Konstabel

    (@konstabel)

    Finally I got what I wanted:

    <?php
      $args = array( 'numberposts' => '1' );
      $recent_posts = wp_get_recent_posts( $args );
    
      foreach( $recent_posts as $recent ){
      echo '<h3><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a></h3>';
      echo $recent["post_content"];
      }
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get and show latest post contents’ is closed to new replies.