Skip to content

Instantly share code, notes, and snippets.

@favrik
Created June 25, 2009 04:01
Show Gist options
  • Save favrik/135672 to your computer and use it in GitHub Desktop.
Save favrik/135672 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: favrik Recent Posts
Plugin URI: http://blog.favrik.com/
Description: Display recent posts by date: 1) grouping by year, month,day, and 2) just the date at the left side of the title. Based on the work by <a href="http://www.ardamis.com">Oliver Baty</a>. <a href=" http://www.ardamis.com/2007/06/25/adding-the-post-date-to-wp_get_archives/">Details are here</a>.
Version: 0.1
Author: Favio Manriquez
Author URI: http://blog.favrik.com
Copyright 2007 Favio Manriquez (email : favio@favrik.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
Most important configuration variable is $group:
0 - Just put the date at the left side.
1 - Group by year, month, and day.
2 - Group in columns New parameters: cols = number of posts per column
cclass = column class
*/
function favrik_recent_posts($args = '') {
global $wp_locale, $wpdb;
$G = array(
'FULL_DATE' => 0,
'DATE_PARTS' => 1,
'COLUMNS' => 2,
);
// params fun
parse_str($args, $r);
$defaults = array(
'group' => 1,
'cols' => 2,
'cclass' => 'column',
'limit' => '10',
'before' => '<li>',
'after' => '</li>',
'show_post_count' => false,
'show_post_date' => true,
'date' => 'F jS, Y',
'order_by' => 'post_date DESC'
);
$r = array_merge($defaults, $r);
extract($r);
// output
$OUTPUT = ''; // plugin output
$outer_date = '';
$inner_date = '';
$date_check = '';
$year = '';
$month = '';
$day = '';
// the query
$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'");
$join = apply_filters('getarchives_join', "");
$qry = "SELECT ID, post_date, post_title, post_name
FROM $wpdb->posts $join
$where ORDER BY $order_by LIMIT $limit";
$posts = $wpdb->get_results($qry);
$post_number = count($posts);
$current_post = 1;
if ($posts) {
foreach ($posts as $post) {
if ($post->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($post);
$post_time = strtotime($post->post_date);
if ($group == $G['COLUMNS']) {
if ($current_post == 1) {
$OUTPUT .= '<div class="'.$cclass.'"><ul>' . "\n";
}
$current_date = date($date, $post_time);
if ($date_check != $current_date) {
$date_check = $current_date;
$outer_date = '<li class="date">' . $current_date . '</li>' . "\n";
}
}
if ($group == $G['FULL_DATE']) { // dates at the side of the post link
$inner_date = '<em class="date">' . date($date, $post_time) . '</em> ';
}
if ($group == $G['DATE_PARTS']) { // grouping by year then month-day
$y = date('Y', $post_time);
if ($year != $y) {
$year = $y;
$outer_date = '<li class="year">' . $year . '</li>';
}
$m = date('F Y', $post_time);
if ($month != $m) {
$month = $m;
$outer_date .= '<li class="month">' . substr($month, 0, -4) . '</li>';
}
$d = date('jS', $post_time);
if ($day != $d) {
$day = $d;
$inner_date = '<em>' . $day . '</em>';
}
}
$text = strip_tags(apply_filters('the_title', $post->post_title));
$OUTPUT .= get_archives_link(
$url, $text, $format, $outer_date . $before . $inner_date, $after
);
if ($group == $G['COLUMNS']) {
if ($current_post % $cols == 0) {
$OUTPUT .= '</ul></div>' ."\n\n";
if ($post_number > $current_post) {
$OUTPUT .= '<div class="'.$cclass.'"><ul>' . "\n";
}
}
}
// Reset dates
$outer_date = '';
$inner_date = '';
}
$current_post += 1;
}
}
echo $OUTPUT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment