Technique #1
This function is useful when you need to display content, excerpt, custom fields, or anything related to the post beyond it’s link and title. If you just need a list of linked titles, see the next technique. Put the following function in functions.php
function recent_posts($no_posts = 10, $excerpts = true) {
global $wpdb;
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
$posts = $wpdb->get_results($request);
if($posts) {
foreach ($posts as $posts) {
$post_title = stripslashes($posts->post_title);
$permalink = get_permalink($posts->ID);
$output .= '<li><h2><a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a></h2>';
if($excerpts) {
$output.= '<br />' . stripslashes($posts->post_excerpt);
}
$output .= '</li>';
}
} else {
$output .= '<li>No posts found</li>';
}
echo $output;
}
Usage
After you’ve made the function. Put the following in the sidebar or wherever you like the recent posts to list..
<?php recent_posts(); ?>
You can give it 2 arguments, the first is the number of posts and the second is whether or not you want to display the excerpts. so recent_posts(2, false) will display the 2 most recent post titles.
Technique #2
<?php wp_get_archives( array(
'type' => 'postbypost', // or daily, weekly, monthly, yearly
'limit' => 10, // maximum number shown
'format' => 'html', // or select (dropdown), link, or custom (then need to also pass before and after params for custom tags
'show_post_count' => false, // show number of posts per link
'echo' => 1 // display results or return array
) ); ?>
Technique #3
More succinct version of #1, which also includes a more standardized query string.
<?php
$recentposts = get_posts('numberposts=12&category=4');
foreach ($recentposts as $post) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
when i upgrade wordpress one version to another new version…
is it the function.php is overwritten by updates..? and need to re-add the above code..??
Please advice…
mans! weh dah dah la tu dow ng website ko.haha..
@chris, thank for this man
quick question – how do you add the date to the function?
thanks
@mansur, no it is not, the functions.php file is a part of your theme which is not updated during a wordpress update, it is only updated when u update your theme.
The functions.php file will only be updated during a wordpress update if you’re using the default wordpress theme even if you may have customized it.
If you’re currently using the default theme, the solution is renaming the theme from the style.css file, you should also rename the theme folder(very important!).
I am a newbie … and I would like to know how you even determine when to implement the above post … cause it sounds cool … but how do you know “WHEN”
Ok I have a crazy question. A client of mine would like to display the latest 3 post on their home page. How would I go about breaking down each post into a seperate div for styling?
The format would look something like below:
Show Recent Post / Show Second Recent Post / Show Third Recent Post
Each of the above are in separate DIV’s Hope that somewhat helps…
Thanks in Advance,
Aaron
its not working for my blog :(
Open your Cpanel or FileZilla.
Find functions.php hit edit. Insert the code provided above
at the bottom of the page and hit save.
Now go back to where you wanted your recent posts to appear
and simply type:
Hope it helps.
@interviewdate post a sample of the code you implemented, maybe i could help (that’s if you haven’t figured it out already)
please help me … I want to show recent posts in my index.php …. not in woordpress
and this is the code i used i to input the posts
but i didn’t know ho to get just the recent posts for eg the 8 recent posts…..
php
$re= “select * from tableaux
LIMIT 0 , 7″;
$k = mysql_query($re);
while($s=mysql_fetch_row($k))
{
$t = substr($s[2] , 0 , 300 );
$t=$t.” …”;
echo ‘
a href=”‘.$s[3].'”title=”‘.$s[1].'” ></a
}
Works great. But how can i show the “Post Date” ???
where I should put the code
<?php recent_posts(); ?>
?Thanks very much these tutorials and loops are great
To include the date just do this:
In the function replace this
$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
with this
$request = "SELECT ID, post_title, post_excerpt, post_date FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
and just add it to your output a bit like this
$output .= '<a href="' . $permalink . '" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '" rel="nofollow">' . htmlspecialchars($post_title) . '(' . htmlspecialchars($post_date) . ')' . '</a>';
Or to format your date in the format of 24th April 2014 change
post_date
in the query toDATE_FORMAT( post_date, '%D %M %Y' ) AS post_date
How can you control the font size?
Thanks you so much it really helps a lot
I have a different page apart from Posts & Pages like – https://example.com/lyrics
& i want to display recent lyrics post from this page. Can you pls tell me, what should i do for this?