Advertisement
alchymyth

category archive posts widget

Sep 14th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Category Archive Posts Widget
  4. Plugin URI: N/A - based on http://jameslao.com/2011/03/24/category-posts-widget-3-2/
  5. Description: Adds a widget that can display posts from category archive's category, or from all categories.
  6. 2012 Hacked by alchymyth to show posts from the category archive's category; all categories otherwise.
  7. Author: originally: James Lao  
  8. Version: n/a
  9. Author URI: http://jameslao.com/
  10. */
  11.  
  12. // Register thumbnail sizes.
  13. if ( function_exists('add_image_size') )
  14. {
  15.     $sizes = get_option('jlao_cat_post_thumb_sizes');
  16.     if ( $sizes )
  17.     {
  18.         foreach ( $sizes as $id=>$size )
  19.             add_image_size( 'cat_post_thumb_size' . $id, $size[0], $size[1], true );
  20.     }
  21. }
  22.  
  23. class CategoryPosts extends WP_Widget {
  24.  
  25. function CategoryPosts() {
  26.     parent::WP_Widget(false, $name='Category Archive Posts');
  27. }
  28.  
  29. /**
  30.  * Displays category posts widget on blog.
  31.  */
  32. function widget($args, $instance) {
  33.     global $post;
  34.     $post_old = $post; // Save the post object.
  35.    
  36.     extract( $args );
  37.    
  38.     $sizes = get_option('jlao_cat_post_thumb_sizes');
  39.    
  40.     // If not title, use the name of the category.
  41.     if( !$instance["title"] ) {
  42. //alchymyth edit: set widget title to 'Category Archive Posts' for option 'use archive category'   
  43.         if( $instance["cat"] == 0 ) { $instance["title"] = 'Category Archive Posts'; } else {
  44.         $category_info = get_category($instance["cat"]);
  45.         $instance["title"] = $category_info->name;
  46.         } //end alchymyth edit
  47.   }
  48.  
  49.   $valid_sort_orders = array('date', 'title', 'comment_count', 'random');
  50.   if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
  51.     $sort_by = $instance['sort_by'];
  52.     $sort_order = (bool) $instance['asc_sort_order'] ? 'ASC' : 'DESC';
  53.   } else {
  54.     // by default, display latest first
  55.     $sort_by = 'date';
  56.     $sort_order = 'DESC';
  57.   }
  58.  
  59. //alchymyth edit: get the category archive's category
  60. if( $instance["cat"] == '0' ) {
  61. global $post;
  62. if( is_category() ) { $use_categories = get_query_var('cat'); } else { $use_categories = 0; }
  63. } else { $use_categories = $instance["cat"]; }
  64. //end of alchymyth edit
  65.  
  66.     // Get array of post info.
  67.   $cat_posts = new WP_Query(
  68.     "showposts=" . $instance["num"] .
  69.     "&cat=" . $use_categories . //alchymyth edit
  70.     "&orderby=" . $sort_by .
  71.     "&order=" . $sort_order
  72.   );
  73.  
  74.     // Excerpt length filter
  75.     $new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
  76.     if ( $instance["excerpt_length"] > 0 )
  77.         add_filter('excerpt_length', $new_excerpt_length);
  78.  
  79.     echo $before_widget;
  80.    
  81.     // Widget title
  82.     echo $before_title;
  83.     if( $instance["title_link"] )
  84.         echo '<a href="' . get_category_link($instance["cat"]) . '">' . $instance["title"] . '</a>';
  85.     else
  86.         echo $instance["title"];
  87.     echo $after_title;
  88.  
  89.     // Post list
  90.     echo "<ul>\n";
  91.    
  92.     while ( $cat_posts->have_posts() )
  93.     {
  94.         $cat_posts->the_post();
  95.     ?>
  96.         <li class="cat-post-item">
  97.             <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  98.            
  99.             <?php
  100.                 if (
  101.                     function_exists('the_post_thumbnail') &&
  102.                     current_theme_supports("post-thumbnails") &&
  103.                     $instance["thumb"] &&
  104.                     has_post_thumbnail()
  105.                 ) :
  106.             ?>
  107.                 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  108.                 <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
  109.                 </a>
  110.             <?php endif; ?>
  111.  
  112.             <?php if ( $instance['date'] ) : ?>
  113.             <p class="post-date"><?php the_time("j M Y"); ?></p>
  114.             <?php endif; ?>
  115.            
  116.             <?php if ( $instance['excerpt'] ) : ?>
  117.             <?php the_excerpt(); ?>
  118.             <?php endif; ?>
  119.            
  120.             <?php if ( $instance['comment_num'] ) : ?>
  121.             <p class="comment-num">(<?php comments_number(); ?>)</p>
  122.             <?php endif; ?>
  123.         </li>
  124.     <?php
  125.     }
  126.    
  127.     echo "</ul>\n";
  128.    
  129.     echo $after_widget;
  130.    
  131.     remove_filter('excerpt_length', $new_excerpt_length);
  132.    
  133.     $post = $post_old; // Restore the post object.
  134.    
  135.     remove_filter('wp_dropdown_cats', 'use_posts_cats');
  136.  
  137. }
  138.  
  139. /**
  140.  * Form processing... Dead simple.
  141.  */
  142. function update($new_instance, $old_instance) {
  143.     /**
  144.      * Save the thumbnail dimensions outside so we can
  145.      * register the sizes easily. We have to do this
  146.      * because the sizes must registered beforehand
  147.      * in order for WP to hard crop images (this in
  148.      * turn is because WP only hard crops on upload).
  149.      * The code inside the widget is executed only when
  150.      * the widget is shown so we register the sizes
  151.      * outside of the widget class.
  152.      */
  153.     if ( function_exists('the_post_thumbnail') )
  154.     {
  155.         $sizes = get_option('jlao_cat_post_thumb_sizes');
  156.         if ( !$sizes ) $sizes = array();
  157.         $sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']);
  158.         update_option('jlao_cat_post_thumb_sizes', $sizes);
  159.     }
  160.    
  161.     return $new_instance;
  162. }
  163.  
  164. /**
  165.  * The configuration form.
  166.  */
  167. function form($instance) {
  168. ?>
  169.         <p>
  170.             <label for="<?php echo $this->get_field_id("title"); ?>">
  171.                 <?php _e( 'Title' ); ?>:
  172.                 <input class="widefat" id="<?php echo $this->get_field_id("title"); ?>" name="<?php echo $this->get_field_name("title"); ?>" type="text" value="<?php echo esc_attr($instance["title"]); ?>" />
  173.             </label>
  174.         </p>
  175.        
  176.         <p>
  177.             <label>
  178.                 <?php _e( 'Category' ); ?>:
  179.                 <?php
  180.                  wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
  181.             </label>
  182.         </p>
  183.        
  184.         <p>
  185.             <label for="<?php echo $this->get_field_id("num"); ?>">
  186.                 <?php _e('Number of posts to show'); ?>:
  187.                 <input style="text-align: center;" id="<?php echo $this->get_field_id("num"); ?>" name="<?php echo $this->get_field_name("num"); ?>" type="text" value="<?php echo absint($instance["num"]); ?>" size='3' />
  188.             </label>
  189.     </p>
  190.  
  191.     <p>
  192.             <label for="<?php echo $this->get_field_id("sort_by"); ?>">
  193.         <?php _e('Sort by'); ?>:
  194.         <select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
  195.           <option value="date"<?php selected( $instance["sort_by"], "date" ); ?>>Date</option>
  196.           <option value="title"<?php selected( $instance["sort_by"], "title" ); ?>>Title</option>
  197.           <option value="comment_count"<?php selected( $instance["sort_by"], "comment_count" ); ?>>Number of comments</option>
  198.           <option value="random"<?php selected( $instance["sort_by"], "random" ); ?>>Random</option>
  199.         </select>
  200.             </label>
  201.     </p>
  202.        
  203.         <p>
  204.             <label for="<?php echo $this->get_field_id("asc_sort_order"); ?>">
  205.         <input type="checkbox" class="checkbox"
  206.           id="<?php echo $this->get_field_id("asc_sort_order"); ?>"
  207.           name="<?php echo $this->get_field_name("asc_sort_order"); ?>"
  208.           <?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
  209.                 <?php _e( 'Reverse sort order (ascending)' ); ?>
  210.             </label>
  211.     </p>
  212.  
  213.         <p>
  214.             <label for="<?php echo $this->get_field_id("title_link"); ?>">
  215.                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("title_link"); ?>" name="<?php echo $this->get_field_name("title_link"); ?>"<?php checked( (bool) $instance["title_link"], true ); ?> />
  216.                 <?php _e( 'Make widget title link' ); ?>
  217.             </label>
  218.         </p>
  219.        
  220.         <p>
  221.             <label for="<?php echo $this->get_field_id("excerpt"); ?>">
  222.                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("excerpt"); ?>" name="<?php echo $this->get_field_name("excerpt"); ?>"<?php checked( (bool) $instance["excerpt"], true ); ?> />
  223.                 <?php _e( 'Show post excerpt' ); ?>
  224.             </label>
  225.         </p>
  226.        
  227.         <p>
  228.             <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
  229.                 <?php _e( 'Excerpt length (in words):' ); ?>
  230.             </label>
  231.             <input style="text-align: center;" type="text" id="<?php echo $this->get_field_id("excerpt_length"); ?>" name="<?php echo $this->get_field_name("excerpt_length"); ?>" value="<?php echo $instance["excerpt_length"]; ?>" size="3" />
  232.         </p>
  233.        
  234.         <p>
  235.             <label for="<?php echo $this->get_field_id("comment_num"); ?>">
  236.                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("comment_num"); ?>" name="<?php echo $this->get_field_name("comment_num"); ?>"<?php checked( (bool) $instance["comment_num"], true ); ?> />
  237.                 <?php _e( 'Show number of comments' ); ?>
  238.             </label>
  239.         </p>
  240.        
  241.         <p>
  242.             <label for="<?php echo $this->get_field_id("date"); ?>">
  243.                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date"); ?>" name="<?php echo $this->get_field_name("date"); ?>"<?php checked( (bool) $instance["date"], true ); ?> />
  244.                 <?php _e( 'Show post date' ); ?>
  245.             </label>
  246.         </p>
  247.        
  248.         <?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?>
  249.         <p>
  250.             <label for="<?php echo $this->get_field_id("thumb"); ?>">
  251.                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("thumb"); ?>" name="<?php echo $this->get_field_name("thumb"); ?>"<?php checked( (bool) $instance["thumb"], true ); ?> />
  252.                 <?php _e( 'Show post thumbnail' ); ?>
  253.             </label>
  254.         </p>
  255.         <p>
  256.             <label>
  257.                 <?php _e('Thumbnail dimensions'); ?>:<br />
  258.                 <label for="<?php echo $this->get_field_id("thumb_w"); ?>">
  259.                     W: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumb_w"); ?>" name="<?php echo $this->get_field_name("thumb_w"); ?>" value="<?php echo $instance["thumb_w"]; ?>" />
  260.                 </label>
  261.                
  262.                 <label for="<?php echo $this->get_field_id("thumb_h"); ?>">
  263.                     H: <input class="widefat" style="width:40%;" type="text" id="<?php echo $this->get_field_id("thumb_h"); ?>" name="<?php echo $this->get_field_name("thumb_h"); ?>" value="<?php echo $instance["thumb_h"]; ?>" />
  264.                 </label>
  265.             </label>
  266.         </p>
  267.         <?php endif; ?>
  268.  
  269. <?php
  270.  
  271. }
  272.  
  273. }
  274.  
  275. add_action( 'widgets_init', create_function('', 'return register_widget("CategoryPosts");') );
  276.  
  277. //alchymyth edit: add top option to dropdown               
  278. add_filter('wp_dropdown_cats', 'use_posts_cats');
  279. function use_posts_cats($list) {
  280. $list = str_replace("class='postform' >", "class='postform' >\n<option class=\"level-0\" value=\"0\">use archive category</option>\n", $list);
  281. return $list; };
  282. //end alchymyth edit
  283.            
  284. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement