Wordpress > Custom wp-archives list by year and category with post count in sidebar
Posted
by David
on Stack Overflow
See other posts from Stack Overflow
or by David
Published on 2010-05-22T19:27:18Z
Indexed on
2010/05/22
19:30 UTC
Read the original article
Hit count: 412
Wordpress
So I have been trying to add a custom sidebar archives section to this Wordpress Theme. I am trying to get two separate yearly archive sections, one for category A and one for category B. I need to get a post count and display it as well, to the left of "articles".
This is the format I have been trying to get in the sidebar:
Category 1
2010 - 5 articles
2009 - 4 articles
2008 - 6 articles
2007 - 5 articles
Category 2
2010 - 8 articles
2009 - 3 articles
2008 - 7 articles
2007 - 5 articles
This code is pulling the yearly archive, but the links to the yearly archives do not filter by category. However, if Category 1 does not have posts in 2008, 2008 would not show. So I feel like I am really close, but there is something wrong here in the code. I have also been unsuccessful in pulling the post count for the year/category either. Here is what I get:
Category 1
2010 - articles (these links all take you to the general yearly archive page, not category specific)
2009 - articles
2007 - articles
Category 2
2010 - articles
2009 - articles
2008 - articles
2007 - articles
Here is are the functions I am using in my functions.php file, any idea what I am doing wrong?
<?php
function company_below_sidebar() {
global $cat;
?>
<div id="press">
<h2>Press</h2>
<ul><?php $cat = '1'; wp_get_archives('type=yearly') ?></ul>
</div>
<div id="news">
<h2>News</h2>
<ul><?php $cat = '4'; wp_get_archives('type=yearly') ?></ul>
</div>
<?php
}
add_action('thematic_betweenmainasides', 'company_below_sidebar');
function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb- >term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}
function customarchives_where( $x ) {
global $wpdb;
global $cat;
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($cat)";
}
add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );
function company_monthly_archive_flexible($input) {
// Get URL from $input
preg_match('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&;//=]+)', $input, $matches);
$url = $matches[0];
// Get content from $input (without the tags)
$content = trim(strip_tags($input));
// Seperate each date element and put it in an array
$dates = explode(" ", $content);
// Get year
$year = $dates[0];
// Customize output format
$format = "<li><a href='$url'><b>$year -</b> articles</a></li>\n";
echo $format;
}
add_filter('get_archives_link','company_monthly_archive_flexible'); ?>
© Stack Overflow or respective owner