How to: Show Recent Articles in WordPress (without plugin / widget)

Similarly with the post to display the recent comments of your WordPress blog, you can even tell your visitors about your last written or latest articles of your blogs.

For this purpose, there are already plenty of plugins and widgets that provide this functionality, but it’s just without the use of additional plugins. A code snippet that shows the last article of your blog, you will find in this article.

wordpress

The code to determine the last item

The following code can be used in the functions.php file to be copied:

function get_latest_posts($limit = 3)
{
$latest_posts = get_posts(‘numberposts=’ . $limit);

$output = ‘<ul>’;
foreach($latest_posts as $post)
{
$post_title = $post->post_title;
$post_permalink = get_permalink($post->ID);

$thumbnail = get_the_post_thumbnail(
$post->ID,
‘thumbnail’,
array(
‘class’ => ”,
‘alt’ => $post_title,
‘title’ => $post_title
)
);

$output .= ‘<li><a href=”‘ . $post_permalink . ‘” title=”Artikel: ‘ . $post_title . ‘”>’;
$output .= $thumbnail . ‘<span>’ . $post_title . ‘</span></a></li>’;
}

$output .= ‘</ul>’;

return $output;
}

To determine the last item we need, as you can see, a single line of SQL, but only the function get_posts () (line 3) with the parameter number posts. Default if when calling get_latest_posts () not otherwise specified, the last written 3 articles are fetched from the database.

The second portion (lines 5-23) the composition is the output. In this example we simply create a list item for each <li> </ li>, which in turn contains a link, the link to the relevant article. The link text consists of our article thumbnail and the title of the article.

The code to display the last items

The following code can be placed anywhere in your WordPress theme. Would you like eg the list of the last 7 articles in the sidebar, you can add the following code example in the sidebar.php to insert.

1.echo  get_latest_posts (7);

Conclusion

The edition you can customize and make your wishes by using CSS.

 

Filed in: Blog, Wordpress Tags: 

You might like:

How to: Show Similar articles in WordPress (without plugin) How to: Show Similar articles in WordPress (without plugin)
How to: Show Latest Comments in WordPress (without plugin/widget) How to: Show Latest Comments in WordPress (without plugin/widget)
How to: Show Top Commentators in WordPress (without plugin) How to: Show Top Commentators in WordPress (without plugin)
How to protect a WordPress blog from hacker attacks, increase security! How to protect a WordPress blog from hacker attacks, increase security!

Leave a Reply

Submit Comment

© 2017 TechieJerry. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.