Jump to content

Recommended Posts

Posted

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?

Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...