web/wp-content/plugins/posts-of-current-category/posts-of-current-category.php
changeset 136 bde1974c263b
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Posts of Current Category
       
     4 Plugin URI: http://anupraj.com.np/home/wp-plugins/display-posts-of-current-category-widget/
       
     5 Description: Lists (Show/Display title) Posts of Current Category and Order Posts by author,content,date,ID, .. etc  Sort ASC/DESC. Go to Appearance>Widgets and Drag "Posts of Current Category" widget to your Sidebar.
       
     6 Version: 0.1
       
     7 Author: Anup Raj
       
     8 Author URI: http://anupraj.com.np/home/
       
     9 // modified by sam 21/01/2010
       
    10 */
       
    11 
       
    12 class posts_cur_cat extends WP_Widget {
       
    13 	function posts_cur_cat() {
       
    14 		$widget_ops = array('description' => __('Display Posts of current category and order it.', 'posts-of-cur-cat') );
       
    15 		//Create widget
       
    16 		$this->WP_Widget('postsofcurrentcategory', __('Posts of Current Category', 'posts-of-cur-cat'), $widget_ops);
       
    17 	}
       
    18 
       
    19   function widget($args, $instance) {
       
    20 	 		extract($args, EXTR_SKIP);
       
    21 			echo $before_widget;
       
    22 			
       
    23 			$title = empty($instance['title']) ? __('', 'posts-of-cur-cat') : apply_filters('widget_title', $instance['title']);
       
    24 			$parameters = array(
       
    25 			  'title' => $title,
       
    26 				'limit' => (int) $instance['show-num'],
       
    27 				'excerpt' => (int) $instance['excerpt-length'],
       
    28 				'AscDesc' => esc_attr($instance['AscDesc']),
       
    29 				'OrderBy' => esc_attr($instance['OrderBy'])
       
    30 				
       
    31 			);
       
    32 			if ( !empty( $title ) ) {
       
    33 		    echo $before_title . $title . $after_title;
       
    34 			};
       
    35 			
       
    36 			//print recent posts
       
    37 			post_of_curCat($parameters);
       
    38 			echo $after_widget;
       
    39 			
       
    40   } //end of widget
       
    41 	
       
    42 	//Update widget options
       
    43   function update($new_instance, $old_instance) {
       
    44 
       
    45 		$instance = $old_instance;
       
    46 		//get old variables
       
    47 		$instance['title'] = esc_attr($new_instance['title']);
       
    48 		$instance['show-num'] = (int) abs($new_instance['show-num']);
       
    49 		
       
    50 		$instance['OrderBy'] = esc_attr($new_instance['OrderBy']);
       
    51 		$instance['AscDesc'] = esc_attr($new_instance['AscDesc']);
       
    52 		
       
    53 		return $instance;
       
    54   } //end of update
       
    55 	
       
    56 	//Widget options form
       
    57   function form($instance) {
       
    58 		
       
    59 		$instance = wp_parse_args(
       
    60 		(array) $instance, 
       
    61 		array(
       
    62 			'title' => __('Posts of Current Category','posts-of-cur-cat'), 
       
    63 			'show-num' => 10,
       
    64 			'AscDesc' => "ASC",
       
    65 			'OrderBy' => "ID"
       
    66 			)
       
    67 		);
       
    68 		
       
    69 		$title = esc_attr($instance['title']);
       
    70 		$show_num = (int) $instance['show-num'];
       
    71 		$AscDesc = esc_attr($instance['AscDesc']);
       
    72 		$OrderBy = esc_attr($instance['OrderBy']);
       
    73 
       
    74 		?>
       
    75 		<p>
       
    76 		   <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:');?> 
       
    77 			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
       
    78 		   </label>
       
    79 		</p>
       
    80 		<p>
       
    81 		   <label for="<?php echo $this->get_field_id('show-num'); ?>"><?php _e('Number of posts to show:');?> 
       
    82 		  <input id="<?php echo $this->get_field_id('show-num'); ?>" name="<?php echo $this->get_field_name('show-num'); ?>" type="text" value="<?php echo $show_num; ?>" size ="3" /><br />
       
    83 			
       
    84 		  </label>
       
    85 	  </p>
       
    86 		<p>
       
    87 			<label for="<?php echo $this->get_field_id('OrderBy'); ?>"> <?php _e('Order By, Post\'s :  ', 'posts-of-cur-cat');?></label>
       
    88 			<select class="select" id="<?php echo $this->get_field_id('OrderBy'); ?>" name="<?php echo $this->get_field_name('OrderBy'); ?>">
       
    89 				<option value="author" <?php if($OrderBy=="author") echo "SELECTED"; ?>>author</option>
       
    90 				<option value="category" <?php if($OrderBy=="category") echo "SELECTED"; ?>>category</option>
       
    91 				<option value="content" <?php if($OrderBy=="content") echo "SELECTED"; ?>>content</option>
       
    92 				<option value="date" <?php if($OrderBy=="date") echo "SELECTED"; ?>>date</option>
       
    93 				<option value="ID" <?php if($OrderBy=="ID") echo "SELECTED"; ?>>ID</option>
       
    94 				<option value="menu_order" <?php if($OrderBy=="menu_order") echo "SELECTED"; ?>>menu_order</option>
       
    95 				<option value="mime_type" <?php if($OrderBy=="mime_type") echo "SELECTED"; ?>>mime_type</option>
       
    96 				<option value="modified" <?php if($OrderBy=="modified") echo "SELECTED"; ?>>modified</option>
       
    97 				<option value="name" <?php if($OrderBy=="name") echo "SELECTED"; ?>>name</option>
       
    98 				<option value="parent" <?php if($OrderBy=="parent") echo "SELECTED"; ?>>parent</option>
       
    99 				<option value="password" <?php if($OrderBy=="password") echo "SELECTED"; ?>>password</option>
       
   100 				<option value="rand" <?php if($OrderBy=="rand") echo "SELECTED"; ?>>rand</option>
       
   101 				<option value="status" <?php if($OrderBy=="status") echo "SELECTED"; ?>>status</option>
       
   102 				<option value="title" <?php if($OrderBy=="title") echo "SELECTED"; ?>>title</option>
       
   103 				<option value="type" <?php if($OrderBy=="type") echo "SELECTED"; ?>>type</option>
       
   104 			</select>
       
   105 		</p>
       
   106 		<p>
       
   107 			<label for="<?php echo $this->get_field_id('AscDesc'); ?>"> <?php _e('Sort Order By : &nbsp; &nbsp; ', 'posts-of-cur-cat');?></label>
       
   108 			<select class="select" id="<?php echo $this->get_field_id('AscDesc'); ?>" name="<?php echo $this->get_field_name('AscDesc'); ?>">
       
   109 				<option value="ASC" <?php if($AscDesc=="ASC") echo "SELECTED"; ?>>ASCending</option>
       
   110 				<option value="DESC" <?php if($AscDesc=="DESC") echo "SELECTED"; ?>>DESCending</option>
       
   111 			</select>
       
   112 		</p>
       
   113    <?php
       
   114   } //end of form
       
   115 }
       
   116 
       
   117 add_action( 'widgets_init', create_function('', 'return register_widget("posts_cur_cat");') );
       
   118 //Register Widget
       
   119 
       
   120  // Show recent posts function
       
   121  function post_of_curCat($args = '') {
       
   122   global $wpdb;
       
   123 	
       
   124 	$defaults = array(
       
   125 		'limit' => 10,
       
   126 		'AscDesc' => "ASC", 
       
   127 		'OrderBy' =>'ID',
       
   128 		
       
   129 	);
       
   130 	$args = wp_parse_args( $args, $defaults );
       
   131 	extract($args);
       
   132 	
       
   133 	$limit = (int) abs($limit);
       
   134 		
       
   135 	if(is_category()){
       
   136 	 $curCategoryID = get_query_var('cat');
       
   137 	}
       
   138 	if (is_single()) {
       
   139 	 $curCategoryID = '';
       
   140 	 foreach (get_the_category() as $catt) {
       
   141 	   $curCategoryID .= $catt->cat_ID.' '; 
       
   142 	 }
       
   143 	 $curCategoryID = str_replace(" ", ",", trim($curCategoryID));
       
   144 	}
       
   145 	if (!intval($curCategoryID)) $curCategoryID='';
       
   146 	
       
   147 	$query = "&category=$curCategoryID&showposts=$limit&orderby=$OrderBy&order=$AscDesc";
       
   148 	$posts = get_posts($query); //get posts
       
   149 	$postlist = '';	
       
   150     foreach ($posts as $post) {
       
   151 		$post_title_s = htmlspecialchars(stripslashes(cache_cutter($post->post_title)));
       
   152 		$post_title_l = htmlspecialchars(stripslashes(text_cutter($post->post_title,200)));
       
   153 		// modif here (cache_cutter(); ) 				
       
   154 		$postlist .= '<li><a href="' . get_permalink($post->ID) . '" title="'.  $post_title_s .'" >' . $post_title_l . ' </a> </li>';
       
   155     }
       
   156 	
       
   157 	echo '<ul class="advanced-recent-posts">';		
       
   158 		echo $postlist;
       
   159 	echo '</ul>';
       
   160  }
       
   161 
       
   162 ?>