Random Post Widget
I added a “Random Post” widget to the sidebar today. I spent a couple minutes at the WordPress Widgets site, but I couldn’t find anything I liked so I made my own in PHP that is stupidly simple.
I defined the randomPost() function in wp-includes/functions.php because I knew it was being included already. You can put it anywhere, or just dump the code directly onto your page.
Random Post Code
function randomPost() {
<div class="side-sec pages">
<h3>Random Post</h3>
<?php
// Get ID, Title, Post, and Date from WordPress DB
$query = "SELECT ID, post_title, post_content, post_date FROM wp_posts WHERE post_type='post' ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
// Use $row for grabbed content
$row = mysql_fetch_array($result);
// Take only the first 150 characters and get rid of HTML from post
$content = substr(strip_tags($row[2]), 0, 150);
// Display using relative dates
echo '<p><a href="?p='.$row[0].'">'.$row[1].'</a> '.lastfm_relative($row[3]).'</p>
<p>'.$content.' [...]</p>';
?>
</div>
}
Since I already use the Last.fm for WordPress plugin, I used their relative time function: lastfm_relative(). If you don’t have/want that plugin, here is the code for the function.
Relative Time Function
function lastfm_relative($time) {
$time_orig = strtotime($time);
$diff = $just = time()-$time_orig;
$months = floor($diff/2592000);
$diff -= $months*2419200;
$weeks = floor($diff/604800);
$diff -= $weeks*604800;
$days = floor($diff/86400);
$diff -= $days*86400;
$hours = floor($diff/3600);
$diff -= $hours*3600;
$minutes = floor($diff/60);
$diff -= $minutes*60;
$seconds = $diff;
// Variables defined
if ($just<=0) {
return 'Just Now!';
} else {
if ($months>0) {
// over a month old, just show date (yyyy/mm/dd format)
return 'on '.date('Y/m/d', $time_orig);
} else {
if ($weeks>0) {
// weeks and days
$relative_date .= ($relative_date?', ':'').$weeks.' '.__('week').($weeks>1?'s':'');
$relative_date .= $days>0?($relative_date?', ':'').$days.' '.__('day').($days>1?'s':''):'';
} elseif ($days>0) {
// days and hours
$relative_date .= ($relative_date?', ':'').$days.' '.__('day').($days>1?'s':'');
$relative_date .= $hours>0?($relative_date?', ':'').$hours.' '.__('hour').($hours>1?'s':''):'';
} elseif ($hours>0) {
// hours and minutes
$relative_date .= ($relative_date?', ':'').$hours.' '.__('hour').($hours>1?'s':'');
$relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' '.__('minute').($minutes>1?'s':''):'';
} elseif ($minutes>0) {
// minutes only
$relative_date .= ($relative_date?', ':'').$minutes.' '.__('minute').($minutes>1?'s':'');
} else {
// seconds only
$relative_date .= ($relative_date?', ':'').$seconds.' '.__('second').($seconds>1?'s':'');
}
}
}
// show relative date and add proper verbiage
return $relative_date.' ago';
}
And that’s it. The relative time function is by far the most complex part of this whole operation. There’s no validation or verification — it assumes that there are posts ready to be grabbed, that your database is accessible, etc. You can see it working on the right side of this page.
Comments
Post a comment