Jump to content
Stef's Coding Community

Wordpress: Add Date to Recent Posts list?


Recommended Posts

I'm using this to display the recent posts in the sidebar:

 

<?php wp_get_archives('type=postbypost&limit=5'); ?>

 

Got that code from here: http://codex.wordpress.org/Template_Tags/wp_get_archives

 

I tried just using the regular recent posts widget, but there seems to be a bug where it shows only a page on the Pages instead of the posts.

 

So, does anyone know how to add the date to the get_archives tag?

Link to post
Share on other sites

this is completely untested in WP but... I was up really early in the morning yesterday writing this to pull in WP posts for a CMSMS project ;)

 

<?php

 

global $db;

 

$wp_query = "SELECT post_date, post_title, post_category, post_name FROM wp_posts WHERE post_type = 'POST' AND post_status ='publish' ORDER BY post_date DESC LIMIT 5";

$result = mysql_query($wp_query) or die("Error: " . mysql_error());

 

while($row = mysql_fetch_array( $result )) {

$date_format = date("Y",strtotime($row["post_date"]));

$post_date = date("m/d/Y",strtotime($row["post_date"]));

$post_title = $row["post_title"];

$post_name = $row["post_name"];

$post_cat = $row["post_category"];

 

echo ''.$post_date.' – '.$post_title.'';

}

 

?>

 

This will produce a list of 5 article dates and titles linked to their full articles like this:

 

02/26/2009 - Post-Title

 

So here is the tricky part. This uses SEO Pretty URLs and you need to know your permalink structure.

 

The example above uses post-year/post-name

 

If yours uses post-month/post-day/post-year/category/post-name then some changes will be needed:

 

1st change is to the line $date_format = date("Y",strtotime($row["post_date"]));

 

change to $date_format = date("m/d/Y",strtotime($row["post_date"]));

 

The second change is to this

 

I would add this into your sidebar template - let me know how it works out.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...