How to get latest article(post) in wordpress.

Get latest 5 post

<?php
$myposts = get_posts('');
foreach($myposts as $post) :
setup_postdata($post);
?>

Get featured image of article

 <?php
the_post_thumbnail('medium'); 
?>

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.

 <?php
 $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)
  array_pop($words);
  return implode(' ', $words);
}

latest post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s