alchymyth

'category posts' hack

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