How to get latest article(post) in wordpress.

How to get latest article(post) in wordpress.

Get latest 5 post

$myposts = get_posts('');
foreach($myposts as $post) :

Get featured image of article


Get post title and its link.

<h3><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h3>

Get limit words of discription.

 $excerpt = get_the_excerpt();
 echo string_limit_words($excerpt,25);

Read more link to the post

<a href="<?php the_permalink() ?>">Read More</a>

<?php endforeach; wp_reset_postdata(); ?>

For limit words put following code in function.php in your theme.

function string_limit_words($string, $word_limit)
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  return implode(' ', $words);

latest post

Leave a Comment