Guest User

Untitled

a guest
Jun 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <?php
  2. function ch_archive_graph($args = ''){
  3. global $wpdb, $wp_locale;
  4.  
  5. //template tag defaults
  6. $defaults = array(
  7. 'graphYear' => '2007',
  8. 'limit' => '13',
  9. 'graphHeight' => '200',
  10. 'xIncrement' => '55'
  11. );
  12.  
  13. $maxHeight = 1;
  14. $scale = 1;
  15.  
  16. $r = wp_parse_args( $args, $defaults );
  17. extract( $r, EXTR_SKIP );
  18.  
  19. if ( '' != $limit ) {
  20. $limit = absint($limit + 1);
  21. $limit = ' LIMIT '.$limit;
  22. }
  23.  
  24. $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
  25.  
  26. $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";
  27. $key = md5($query);
  28. $cache = wp_cache_get( 'ch_archive_graph' , 'general');
  29.  
  30. $output = '<dl class="barGraph '.$graphYear.'">';
  31.  
  32. if ( !isset( $cache[ $key ] ) ) {
  33. $arcresults = $wpdb->get_results($query);
  34. $cache[ $key ] = $arcresults;
  35. wp_cache_add( 'ch_archive_graph', $cache, 'general' );
  36. } else {
  37. $arcresults = $cache[ $key ];
  38. }
  39. if ( $arcresults ) {
  40.  
  41. //Loop through to find the highest number of posts
  42. foreach ( (array) $arcresults as $arcresult ) {
  43. //number of posts in a month
  44. $total = $arcresult->posts;
  45. if($maxHeight < $total) $maxHeight = $total;
  46. }
  47.  
  48. //Reverse the months
  49. $arcresults = array_reverse($arcresults);
  50. foreach ( (array) $arcresults as $arcresult ) {
  51. //Limit to one year
  52. if($arcresult->year == $graphYear){
  53. //Get month name, then appreviate, delete the second line if you want full month names
  54. $month = sprintf(__('%1$s'), $wp_locale->get_month($arcresult->month));
  55. $month = sprintf(__('%1$s'), $wp_locale->get_month_abbrev($month));
  56. //Get archive link
  57. $url = get_month_link( $arcresult->year, $arcresult->month );
  58. //Number of posts in the month
  59. $num_posts = $arcresult->posts;
  60.  
  61. //Determine the scale and the height of the bar
  62. $scale = $graphHeight / $maxHeight;
  63. $height = ($arcresult->posts * $scale);
  64.  
  65. //Put it all together
  66. $output .= "<dt class='label' style='left: ".$xOffset."px;'><a href=".$url.">".$month."</a></dt>";
  67. $output .= "<dd class='bar ".$month."' style='height: ".$height."px; left: ".$xOffset."px;' title=".$month.">".$num_posts."</dd>";
  68.  
  69. //Increase the offset for the next bar
  70. $xOffset = $xOffset + $xIncrement;
  71. }
  72. }
  73. }
  74.  
  75. $output .= "</dl>";
  76. echo $output;
  77.  
  78. }
Add Comment
Please, Sign In to add comment