public
Last active

WordPress Graphical Post Count Visualization

  • Download Gist
wp-archive-style.css
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#sidebar {
height: 250px;
}
.archiveYear {
width: 100px;
background-color: #000;
padding: 0;
margin: 0;
float: left;
height: 250px;
}
.archiveYear h4
, .archiveYear h4 a
, .archiveYear h4 a:visited
, .archiveYear h4 a:hover
{
background-color: #000;
padding: 0.5em;
margin: 0;
color: #fff;
text-align: left;
font-size: small;
font-weight: normal;
text-decoration: none;
}
.archiveYear ul {
padding: 0;
margin: 0;
list-style-type: none;
font-size: small;
}
.archiveYear ul li {
padding: 0 0.5em;
text-align: left;
}
.archiveYear ul li a {
text-decoration: none;
/* black.gif is a 400 pixel by 400 pixel of solid black */
background-image: url(images/black.gif);
background-repeat: no-repeat;
background-position: -400px 0;
color: #fff;
background-color: #00c;
display: block;
padding: 0.1em 0 0.1em 0.5em;
cursor: pointer;
}
wp-archive.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<div id="sidebar">
 
<?php
 
/* show archive information */
$args = array(
'type' => 'monthly',
'format' => 'custom',
'show_post_count' => true,
'echo' => 0 );
$resulthtml = wp_get_archives($args);
$archives = array_map('trim', explode("\n", $resulthtml));
$archiveMonths = array();
foreach($archives as $item) {
if (trim($item)!='') {
$archiveMonths[] = trim($item);
}
}
$archiveMonths = array_reverse($archiveMonths);
 
$archiveLinks = array();
$yearCount = array();
 
foreach ($archiveMonths as $link) {
// link looks like:
// <a href='http://artlung.com/blog/2001/02/' title='February 2001'>February 2001</a>&nbsp;(9)
// we want it to look like:
// <a href='http://artlung.com/blog/2001/02/' title='February 2001' style='background-position: -9px'>February</a>
// and we need the year;
// and we need the count;
$cssInitial = 400;
$temp = explode('</a>', $link);
// [0] <a href='http://artlung.com/blog/2001/02/' title='February 2001'>February 2001
// [1] &nbsp;(9)
$count = (int)str_replace(array('&nbsp;(', '('),'',$temp[1]);
// 0 ... -400
// 5 ... -395
// 10 .. -390
// 20 .. -380
$cssCount = ($count) - $cssIniital;
$year = (int)substr($temp[0],-4);
$yearCount[$year] += $count;
 
$newLink = str_replace('\'>', ", {$count} posts.' style='background-position: {$cssCount}px 0;'>", substr($temp[0], 0, -5)) . "</a>";
$archiveLinks[$year][] = $newLink;//str_replace($just_text, $month, $link);
}
foreach ($archiveLinks as $k => $v) {
print '<div class="archiveYear">';
print "<h4><a href=\"/blog/{$k}/\" title=\"{$yearCount[$k]} posts in the year {$k}.\">{$k}</a></h4>";
print '<ul><li>' . implode("</li>\n<li>", $v) . '</li></ul>';
print '</div>';
}
 
?>
</div>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.