Skip to content

Instantly share code, notes, and snippets.

@stammy
Created October 29, 2008 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stammy/20787 to your computer and use it in GitHub Desktop.
Save stammy/20787 to your computer and use it in GitHub Desktop.
function to list all wordpress blog entries, by month.. for archives
<?php function list_articles(){
global $month, $wpdb, $wp_version;
$now = current_time('mysql');
if(version_compare($wp_version, '2.1', '<')){$current_posts = "post_date < '$now'";} else {$current_posts = "post_type='post'";}
$arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts . " WHERE post_status='publish' AND $current_posts AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");
if($arcresults){
foreach($arcresults as $arcresult) {
$url = get_month_link($arcresult->year, $arcresult->month);
$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
echo get_archives_link($url, $text, '','<strong>','</strong>');
$thismonth = zeroise($arcresult->month,2);
$thisyear = $arcresult->year;
$arcresults2 = $wpdb->get_results("SELECT ID, post_date, post_title, comment_status FROM " . $wpdb-> posts . " WHERE post_date LIKE '$thisyear-$thismonth-%' AND $current_posts AND post_status='publish' AND post_password='' ORDER BY post_date DESC");
if ($arcresults2) {
echo "<ul class=\"articles_list\">\n";
foreach ($arcresults2 as $arcresult2) {
if ($arcresult2->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($arcresult2->ID);
$arc_title = $arcresult2->post_title;
if ($arc_title) $text = strip_tags($arc_title);
else $text = $arcresult2->ID;
$title_text = wp_specialchars($text, 1);
echo '<li>' . mysql2date('d', $arcresult2->post_date). ': ' . "<a href='$url' title='$title_text'>".wptexturize($text)."</a>";
$comments_count = $wpdb->get_var("SELECT COUNT(comment_id) FROM " . $wpdb->comments . " WHERE comment_post_ID=" . $arcresult2->ID . " AND comment_approved='1'");
if ($arcresult2->comment_status == "open" OR $comments_count > 0) echo ' (' . $comments_count . ')';
echo '</li>';
}
}
echo '</ul>';
}
}
}
}?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment