Advertisement
Guest User

plugin widget category list with posts

a guest
Sep 30th, 2010
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2. /**
  3. Plugin Name: Category List with Posts Widget
  4. Plugin URI:
  5. Description: Category List with Posts and Widget Options
  6. Author: alchymyth
  7. Version: 1.a 09/2010
  8. Author URI: http://www.transformationpowertools.com
  9. */
  10.  
  11. /* Add our function to the widgets_init hook. */
  12. add_action( 'widgets_init', 'cat_posts_list_load_widgets' );
  13.  
  14. /* Function that registers our widget. */
  15. function cat_posts_list_load_widgets() {    register_widget( 'cat_posts_list_Widget' ); }
  16.  
  17.  
  18. class cat_posts_list_Widget extends WP_Widget {
  19. function cat_posts_list_Widget() {
  20.         /* Widget settings. */
  21.         $widget_ops = array( 'classname' => 'widget_categories cat_posts_list', 'description' => 'Category List with a Number of Posts and Widget Options.' );
  22.  
  23.         /* Widget control settings. */
  24.         $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'cat_posts_list-widget' );
  25.  
  26.         /* Create the widget. */
  27.         $this->WP_Widget( 'cat_posts_list-widget', 'Category Posts List', $widget_ops, $control_ops );
  28.     }
  29.    
  30.     function widget( $args, $instance ) {
  31.         extract( $args );
  32.  
  33.         /* User-selected settings. */
  34.         $title = apply_filters('widget_title', $instance['title'] );
  35.        
  36.         $shownumbers = isset( $instance['shownumbers'] ) ? $instance['shownumbers'] : true;
  37.        
  38.         $numberposts = empty( $instance['numberposts'] ) ? '' : $instance['numberposts'];
  39.  
  40.         /* Before widget (defined by themes). */
  41.         echo $before_widget;
  42.  
  43.         /* Title of widget (before and after defined by themes). */
  44.         if ( $title )
  45.             echo $before_title . $title . $after_title;
  46.  
  47. /* the actual function. */
  48.        
  49. echo '<ul>';   
  50. // get all site categories;
  51. $categories = get_categories();
  52. foreach($categories as $category) {
  53. echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
  54. if($shownumbers) echo ' (' . $category->category_count . ')'; //show category count
  55. echo '<ul>';
  56. // get set number of posts per category;
  57. $posts = get_posts('category_name='.$category->name.'&numberposts='.$numberposts);
  58.   if($posts) :
  59.     foreach($posts as $post) {
  60.     setup_postdata($post);
  61.     echo '<li>';
  62.     echo '<a href="' . get_permalink($post->ID) . '" title="link to ' . get_the_title($post->ID) . '">' . get_the_title($post->ID) . '</a>';
  63.     echo '</li>';
  64.     }
  65.   endif;
  66.  
  67. echo '</ul></li>';
  68. } //end 'foreach($categories as $category)'
  69. echo '</ul>';
  70.  
  71. wp_reset_query();
  72.  
  73. /* end of the actual function */
  74.  
  75.         /* After widget (defined by themes). */
  76.         echo $after_widget;
  77.     }
  78.  
  79. function update( $new_instance, $old_instance ) {
  80.         $instance = $old_instance;
  81.  
  82.         /* Strip tags (if needed) and update the widget settings. */
  83.         $instance['title'] = strip_tags( $new_instance['title'] );
  84.        
  85.         $instance['shownumbers'] = $new_instance['shownumbers'] ? 1 : 0;
  86.        
  87.         $instance['numberposts'] = strip_tags( $new_instance['numberposts'] );
  88.  
  89.         return $instance;
  90.     }
  91.    
  92. function form( $instance ) {
  93.  
  94.         //Defaults
  95.         $instance = wp_parse_args( (array) $instance, array( 'shownumbers' => false, 'title' => '', 'numberposts' => '5') );
  96.         $title = esc_attr( $instance['title'] );
  97.         $numberposts = esc_attr( $instance['numberposts'] );
  98.     ?>
  99.        
  100. <p>
  101.             <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
  102.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" />
  103.         </p>
  104.  
  105.        
  106.     <p>
  107.             <label for="<?php echo $this->get_field_id('numberposts'); ?>"><?php _e( 'Max Posts per Category:' ); ?></label> <input type="text" value="<?php echo $numberposts; ?>" name="<?php echo $this->get_field_name('numberposts'); ?>" id="<?php echo $this->get_field_id('numberposts'); ?>" class="widefat" />
  108.             <br />
  109.            
  110.         </p>
  111.         <p>
  112.             <input class="checkbox" type="checkbox" <?php checked( $instance['shownumbers'], true ); ?> id="<?php echo $this->get_field_id( 'shownumbers' ); ?>" name="<?php echo $this->get_field_name( 'shownumbers' ); ?>" />
  113.             <label for="<?php echo $this->get_field_id( 'shownumbers' ); ?>">Show Post Count for Category?</label>
  114.         </p>       
  115.                
  116.         <?php
  117.     }
  118. }
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement