Advertisement
alchymyth

rel cat posts widget 2

Sep 21st, 2012
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.11 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Related Category 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, a single post's categories, or otherwise from all categories. or from one selected category.
  6. 2012 Hacked by alchymyth to show posts from the category archive's category or single posts's categories; all categories otherwise. or the selected category.
  7. Author: James Lao (originally - hack by alchymyth)
  8. Version: n/a.2
  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 RelCategoryPosts extends WP_Widget {
  24.  
  25. function RelCategoryPosts() {
  26.     parent::WP_Widget(false, $name='Related Category 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 'Related Category Posts' for option 'use related category'   
  43.         if( $instance["cat"] == 0 ) { $instance["title"] = 'Related Category 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 related categories - either the category archive's category, or the single post's categories
  60. if( $instance["cat"] == '0' ) {
  61. global $post;
  62.   if( is_category() ) { $use_categories = get_query_var('cat');
  63.   } elseif( is_single() ) { $use_categories = ''; foreach( get_the_category($post->ID) as $cat ) { $use_categories .= $cat->term_id.','; }
  64.   } else { $use_categories = 0;
  65.   }
  66. } else { $use_categories = $instance["cat"]; }
  67. //end of alchymyth edit
  68.  
  69.     // Get array of post info.
  70.   $cat_posts = new WP_Query(
  71.     "showposts=" . $instance["num"] .
  72.     "&cat=" . $use_categories . //alchymyth edit
  73.     "&orderby=" . $sort_by .
  74.     "&order=" . $sort_order
  75.   );
  76.  
  77.     // Excerpt length filter
  78.     $new_excerpt_length = create_function('$length', "return " . $instance["excerpt_length"] . ";");
  79.     if ( $instance["excerpt_length"] > 0 )
  80.         add_filter('excerpt_length', $new_excerpt_length);
  81.  
  82.     echo $before_widget;
  83.    
  84.     // Widget title
  85.     echo $before_title;
  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.    
  132.     remove_filter('excerpt_length', $new_excerpt_length);
  133.    
  134.     $post = $post_old; // Restore the post object.
  135.    
  136.     remove_filter('wp_dropdown_cats', 'use_posts_cats');
  137.  
  138. }
  139.  
  140. /**
  141.  * Form processing... Dead simple.
  142.  */
  143. function update($new_instance, $old_instance) {
  144.     /**
  145.      * Save the thumbnail dimensions outside so we can
  146.      * register the sizes easily. We have to do this
  147.      * because the sizes must registered beforehand
  148.      * in order for WP to hard crop images (this in
  149.      * turn is because WP only hard crops on upload).
  150.      * The code inside the widget is executed only when
  151.      * the widget is shown so we register the sizes
  152.      * outside of the widget class.
  153.      */
  154.     if ( function_exists('the_post_thumbnail') )
  155.     {
  156.         $sizes = get_option('jlao_cat_post_thumb_sizes');
  157.         if ( !$sizes ) $sizes = array();
  158.         $sizes[$this->id] = array($new_instance['thumb_w'], $new_instance['thumb_h']);
  159.         update_option('jlao_cat_post_thumb_sizes', $sizes);
  160.     }
  161.    
  162.     return $new_instance;
  163. }
  164.  
  165. /**
  166.  * The configuration form.
  167.  */
  168. function form($instance) {
  169. ?>
  170.         <p>
  171.             <label for="<?php echo $this->get_field_id("title"); ?>">
  172.                 <?php _e( 'Title' ); ?>:
  173.                 <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"]); ?>" />
  174.             </label>
  175.         </p>
  176.        
  177.         <p>
  178.             <label>
  179.                 <?php add_filter('wp_dropdown_cats', 'use_related_posts_cats'); ?>
  180.                 <?php _e( 'Category' ); ?>:
  181.                 <?php
  182.                  wp_dropdown_categories( array( 'name' => $this->get_field_name("cat"), 'selected' => $instance["cat"] ) ); ?>
  183.                 <?php remove_filter('wp_dropdown_cats', 'use_related_posts_cats'); ?>
  184.             </label>
  185.         </p>
  186.        
  187.         <p>
  188.             <label for="<?php echo $this->get_field_id("num"); ?>">
  189.                 <?php _e('Number of posts to show'); ?>:
  190.                 <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 ($instance["num"] < 0 ?'-':''); echo absint($instance["num"]); ?>" size='3' />
  191.             </label>
  192.     </p>
  193.  
  194.     <p>
  195.             <label for="<?php echo $this->get_field_id("sort_by"); ?>">
  196.         <?php _e('Sort by'); ?>:
  197.         <select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
  198.           <option value="date"<?php selected( $instance["sort_by"], "date" ); ?>>Date</option>
  199.           <option value="title"<?php selected( $instance["sort_by"], "title" ); ?>>Title</option>
  200.           <option value="comment_count"<?php selected( $instance["sort_by"], "comment_count" ); ?>>Number of comments</option>
  201.           <option value="random"<?php selected( $instance["sort_by"], "random" ); ?>>Random</option>
  202.         </select>
  203.             </label>
  204.     </p>
  205.        
  206.         <p>
  207.             <label for="<?php echo $this->get_field_id("asc_sort_order"); ?>">
  208.         <input type="checkbox" class="checkbox"
  209.           id="<?php echo $this->get_field_id("asc_sort_order"); ?>"
  210.           name="<?php echo $this->get_field_name("asc_sort_order"); ?>"
  211.           <?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
  212.                 <?php _e( 'Reverse sort order (ascending)' ); ?>
  213.             </label>
  214.     </p>
  215.    
  216.         <p>
  217.             <label for="<?php echo $this->get_field_id("excerpt"); ?>">
  218.                 <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 ); ?> />
  219.                 <?php _e( 'Show post excerpt' ); ?>
  220.             </label>
  221.         </p>
  222.        
  223.         <p>
  224.             <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
  225.                 <?php _e( 'Excerpt length (in words):' ); ?>
  226.             </label>
  227.             <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" />
  228.         </p>
  229.        
  230.         <p>
  231.             <label for="<?php echo $this->get_field_id("comment_num"); ?>">
  232.                 <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 ); ?> />
  233.                 <?php _e( 'Show number of comments' ); ?>
  234.             </label>
  235.         </p>
  236.        
  237.         <p>
  238.             <label for="<?php echo $this->get_field_id("date"); ?>">
  239.                 <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 ); ?> />
  240.                 <?php _e( 'Show post date' ); ?>
  241.             </label>
  242.         </p>
  243.        
  244.         <?php if ( function_exists('the_post_thumbnail') && current_theme_supports("post-thumbnails") ) : ?>
  245.         <p>
  246.             <label for="<?php echo $this->get_field_id("thumb"); ?>">
  247.                 <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 ); ?> />
  248.                 <?php _e( 'Show post thumbnail' ); ?>
  249.             </label>
  250.         </p>
  251.         <p>
  252.             <label>
  253.                 <?php _e('Thumbnail dimensions'); ?>:<br />
  254.                 <label for="<?php echo $this->get_field_id("thumb_w"); ?>">
  255.                     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"]; ?>" />
  256.                 </label>
  257.                
  258.                 <label for="<?php echo $this->get_field_id("thumb_h"); ?>">
  259.                     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"]; ?>" />
  260.                 </label>
  261.             </label>
  262.         </p>
  263.         <?php endif; ?>
  264.  
  265. <?php
  266.  
  267. }
  268.  
  269. }
  270.  
  271. add_action( 'widgets_init', create_function('', 'return register_widget("RelCategoryPosts");') );
  272.  
  273. //alchymyth edit: add top option to dropdown               
  274. function use_related_posts_cats($list) {
  275. $list = str_replace("class='postform' >", "class='postform' >\n<option class=\"level-0\" value=\"0\">use related categories</option>\n", $list);
  276. return $list; };
  277. //end alchymyth edit
  278.            
  279. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement