Advertisement
beshur

better-archives-widget update

Feb 12th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. <?php
  2.  
  3. // use widgets_init action hook to execute custom function
  4. add_action( 'widgets_init', 'baw_widgetarchives_register_widgets' );
  5.  
  6.  //register our widget
  7. function baw_widgetarchives_register_widgets() {
  8.     register_widget( 'baw_widgetarchives_widget_my_archives' );
  9. }
  10.  
  11. //boj_widget_my_archives class
  12. class baw_widgetarchives_widget_my_archives extends WP_Widget {
  13.  
  14.     //process the new widget
  15.     function baw_widgetarchives_widget_my_archives() {
  16.         $widget_ops = array(
  17.             'classname' => 'baw_widgetarchives_widget_class',
  18.             'description' => 'Display links to archives grouped by year then month.'
  19.             );
  20.         $this->WP_Widget( 'baw_widgetarchives_widget_my_archives', 'Custom Archives Widget', $widget_ops );
  21.     }
  22.  
  23.      //build the widget settings form
  24.     function form( $instance ) {
  25.         $defaults = array( 'title' => 'archives' );
  26.         $instance = wp_parse_args( (array) $instance, $defaults );
  27.         $title = $instance['title'];
  28.         $exclude = $instance['exclude'];
  29.  
  30.         ?>
  31.             <p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>"  type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
  32.             <p>Exclude Tag: <input placeholder="One tag ID" class="widefat" name="<?php echo $this->get_field_name( 'exclude' ); ?>"  type="text" value="<?php echo esc_attr( $exclude ); ?>" /></p>
  33.         <?php
  34.     }
  35.  
  36.     //save the widget settings
  37.     function update( $new_instance, $old_instance ) {
  38.         $instance = $old_instance;
  39.         $instance['title'] = strip_tags( $new_instance['title'] );
  40.         $instance['exclude'] = strip_tags( $new_instance['exclude'] );
  41.  
  42.         return $instance;
  43.     }
  44.  
  45.     //display the widget
  46.     function widget( $args, $instance ) {
  47.         extract( $args );
  48.  
  49.         echo $before_widget;
  50.         $title = apply_filters( 'widget_title', $instance['title'] );
  51.         $exclude = $instance['exclude'];
  52.  
  53.         if ( ! empty( $title ) ) {
  54.             echo $before_title . $title . $after_title;
  55.         };
  56.          
  57.          // years - months
  58.     global $wpdb;
  59.         $prevYear = "";
  60.         $currentYear = "";
  61.  
  62.         $months = $wpdb->get_results("SELECT DISTINCT DATE_FORMAT(post_date, '%b') AS month, MONTH(post_date) AS numMonth, YEAR( post_date ) AS year, COUNT( id ) AS post_count FROM $wpdb->posts WHERE ID NOT IN (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ('".$exclude."') ) AND post_status = 'publish' AND post_date <= now( ) AND post_type = 'post'GROUP BY month , year ORDER BY post_date DESC" );
  63.         if ( $months) {
  64.         echo '<ul>';
  65.        
  66.  
  67.             foreach ( $months as $month ) {
  68.                 $currentYear = $month->year;
  69.                 if ( ( $currentYear != $prevYear ) && ( $prevYear != "" ) ) { echo "</ul></li>"; }
  70.                 if ( $currentYear != $prevYear ) {
  71.                 ?>
  72.                 <li class="baw-year"><a href="<?php echo get_year_link( $month->year ); ?>"><?php echo $month->year; ?></a>
  73.                 <ul class="baw-months">
  74.                 <?php
  75.                 } ?>
  76.                 <li class="baw-month"><a href="<?php echo get_month_link( $month->year, $month->numMonth ); ?>"><?php echo $month->month; ?><?php echo ' ' . $month->year; ?></a></li>
  77.                 <?php
  78.                 $prevYear = $month->year;
  79.             }//end foreach
  80.         }
  81.         ?>
  82.         </ul></li><?php
  83.         echo '</ul>';
  84.         echo $after_widget;
  85.     }
  86. }
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement