Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function ch_archive_graph($args = ''){
- global $wpdb, $wp_locale;
- //template tag defaults
- $defaults = array(
- 'graphYear' => '2007',
- 'limit' => '13',
- 'graphHeight' => '200',
- 'xIncrement' => '55'
- );
- $maxHeight = 1;
- $scale = 1;
- $r = wp_parse_args( $args, $defaults );
- extract( $r, EXTR_SKIP );
- if ( '' != $limit ) {
- $limit = absint($limit + 1);
- $limit = ' LIMIT '.$limit;
- }
- $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
- $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
- $key = md5($query);
- $cache = wp_cache_get( 'ch_archive_graph' , 'general');
- $output = '<dl class="barGraph '.$graphYear.'">';
- if ( !isset( $cache[ $key ] ) ) {
- $arcresults = $wpdb->get_results($query);
- $cache[ $key ] = $arcresults;
- wp_cache_add( 'ch_archive_graph', $cache, 'general' );
- } else {
- $arcresults = $cache[ $key ];
- }
- if ( $arcresults ) {
- //Loop through to find the highest number of posts
- foreach ( (array) $arcresults as $arcresult ) {
- //number of posts in a month
- $total = $arcresult->posts;
- if($maxHeight < $total) $maxHeight = $total;
- }
- //Reverse the months
- $arcresults = array_reverse($arcresults);
- foreach ( (array) $arcresults as $arcresult ) {
- //Limit to one year
- if($arcresult->year == $graphYear){
- //Get month name, then appreviate, delete the second line if you want full month names
- $month = sprintf(__('%1$s'), $wp_locale->get_month($arcresult->month));
- $month = sprintf(__('%1$s'), $wp_locale->get_month_abbrev($month));
- //Get archive link
- $url = get_month_link( $arcresult->year, $arcresult->month );
- //Number of posts in the month
- $num_posts = $arcresult->posts;
- //Determine the scale and the height of the bar
- $scale = $graphHeight / $maxHeight;
- $height = ($arcresult->posts * $scale);
- //Put it all together
- $output .= "<dt class='label' style='left: ".$xOffset."px;'><a href=".$url.">".$month."</a></dt>";
- $output .= "<dd class='bar ".$month."' style='height: ".$height."px; left: ".$xOffset."px;' title=".$month.">".$num_posts."</dd>";
- //Increase the offset for the next bar
- $xOffset = $xOffset + $xIncrement;
- }
- }
- }
- $output .= "</dl>";
- echo $output;
- }
Add Comment
Please, Sign In to add comment