Pascut
Pascut

Reputation: 3434

Wordpress function to show last three posts -> excerpt not working good

I have a php function that should print last three posts: title and excerpt.

For the first post printed there is no excerpt.

Here is the code:

  $posts = wp_get_recent_posts( array('numberposts' => 3, 'post_status' => 'publish')  );
  foreach ($posts as $post)
  {
    setup_postdata($post);
    echo "<h2 style='font-size:18px'>".$post['post_title']."</h2>";
    if($post['post_excerpt']) 
         echo $post['post_excerpt']." <a href='".get_permalink($post['ID'])."'     style='font-size: 17px;'><b>Continue...</b></a>";
    else echo 'no excerpt';
    echo "<br><br />";
  }

I want to shoe the excerpt for the first post printed too (the last posted one). What is wrong in my code? Why it's not working for the first post printed?

Upvotes: 0

Views: 1645

Answers (1)

Xhynk
Xhynk

Reputation: 13840

Your loop is strange. Why not just use a regular WP_Query() loop?

<?php

// The Query
$the_query = new WP_Query( 'posts_per_page=3' );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    the_excerpt();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

or if you are MARRIED to yours, do var_dump( $posts ) and see what variable contains the string you're looking for.

Upvotes: 1

Related Questions