author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/* |
|
3 |
Plugin Name: Posts of Current Category |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
4 |
Plugin URI: http://anupraj.com.np/index.php/display-posts-of-current-category-widget/68 |
136 | 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. |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
6 |
Version: 0.4 |
136 | 7 |
Author: Anup Raj |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
8 |
Author URI: http://anupraj.com.np/ |
136 | 9 |
*/ |
10 |
||
11 |
class posts_cur_cat extends WP_Widget { |
|
12 |
function posts_cur_cat() { |
|
13 |
$widget_ops = array('description' => __('Display Posts of current category and order it.', 'posts-of-cur-cat') ); |
|
14 |
//Create widget |
|
15 |
$this->WP_Widget('postsofcurrentcategory', __('Posts of Current Category', 'posts-of-cur-cat'), $widget_ops); |
|
16 |
} |
|
17 |
||
18 |
function widget($args, $instance) { |
|
19 |
extract($args, EXTR_SKIP); |
|
20 |
||
21 |
$title = empty($instance['title']) ? __('', 'posts-of-cur-cat') : apply_filters('widget_title', $instance['title']); |
|
22 |
$parameters = array( |
|
23 |
'title' => $title, |
|
24 |
'limit' => (int) $instance['show-num'], |
|
25 |
'excerpt' => (int) $instance['excerpt-length'], |
|
26 |
'AscDesc' => esc_attr($instance['AscDesc']), |
|
27 |
'OrderBy' => esc_attr($instance['OrderBy']) |
|
28 |
||
29 |
); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
30 |
echo $before_title; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
echo '<a href="'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
32 |
echo get_permalink($post->post_parent); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
33 |
echo '" tile="'; |
136 | 34 |
if ( !empty( $title ) ) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
35 |
echo $title; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
36 |
echo '">'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
37 |
echo $title; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
38 |
}else{ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
39 |
if($post->post_parent) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
40 |
$parent_title = get_the_title($post->post_parent); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
41 |
echo $parent_title; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
42 |
echo '">'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
43 |
echo $parent_title; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
44 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
45 |
wp_title(''); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
46 |
echo '">'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
47 |
wp_title(''); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
48 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
49 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
50 |
echo '</a>'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
51 |
echo $after_title; |
136 | 52 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
53 |
echo $before_widget; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
54 |
if (is_page()){ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
/* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
if($post->post_parent){ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&sort_column=menu_order"); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
58 |
}else{ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&sort_column=menu_order"); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
if ($children && !$s) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
//echo '<ul>'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
63 |
echo $children; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
64 |
//echo '</ul>'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
65 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
67 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
68 |
if(is_category()){//print recent posts |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
69 |
post_of_curCat($parameters); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
70 |
} |
136 | 71 |
echo $after_widget; |
72 |
||
73 |
} //end of widget |
|
74 |
||
75 |
//Update widget options |
|
76 |
function update($new_instance, $old_instance) { |
|
77 |
||
78 |
$instance = $old_instance; |
|
79 |
//get old variables |
|
80 |
$instance['title'] = esc_attr($new_instance['title']); |
|
81 |
$instance['show-num'] = (int) abs($new_instance['show-num']); |
|
82 |
||
83 |
$instance['OrderBy'] = esc_attr($new_instance['OrderBy']); |
|
84 |
$instance['AscDesc'] = esc_attr($new_instance['AscDesc']); |
|
85 |
||
86 |
return $instance; |
|
87 |
} //end of update |
|
88 |
||
89 |
//Widget options form |
|
90 |
function form($instance) { |
|
91 |
||
92 |
$instance = wp_parse_args( |
|
93 |
(array) $instance, |
|
94 |
array( |
|
95 |
'title' => __('Posts of Current Category','posts-of-cur-cat'), |
|
96 |
'show-num' => 10, |
|
97 |
'AscDesc' => "ASC", |
|
98 |
'OrderBy' => "ID" |
|
99 |
) |
|
100 |
); |
|
101 |
||
102 |
$title = esc_attr($instance['title']); |
|
103 |
$show_num = (int) $instance['show-num']; |
|
104 |
$AscDesc = esc_attr($instance['AscDesc']); |
|
105 |
$OrderBy = esc_attr($instance['OrderBy']); |
|
106 |
||
107 |
?> |
|
108 |
<p> |
|
109 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:');?> |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
110 |
<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; ?>" /><br /> |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
111 |
<em>Leave this field blank, if you want to display Parent's Post's or Page's Title on widget heading</em> |
136 | 112 |
</label> |
113 |
</p> |
|
114 |
<p> |
|
115 |
<label for="<?php echo $this->get_field_id('show-num'); ?>"><?php _e('Number of posts to show:');?> |
|
116 |
<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 /> |
|
117 |
|
|
118 |
</label> |
|
119 |
</p> |
|
120 |
<p> |
|
121 |
<label for="<?php echo $this->get_field_id('OrderBy'); ?>"> <?php _e('Order By, Post\'s : ', 'posts-of-cur-cat');?></label> |
|
122 |
<select class="select" id="<?php echo $this->get_field_id('OrderBy'); ?>" name="<?php echo $this->get_field_name('OrderBy'); ?>"> |
|
123 |
<option value="author" <?php if($OrderBy=="author") echo "SELECTED"; ?>>author</option> |
|
124 |
<option value="category" <?php if($OrderBy=="category") echo "SELECTED"; ?>>category</option> |
|
125 |
<option value="content" <?php if($OrderBy=="content") echo "SELECTED"; ?>>content</option> |
|
126 |
<option value="date" <?php if($OrderBy=="date") echo "SELECTED"; ?>>date</option> |
|
127 |
<option value="ID" <?php if($OrderBy=="ID") echo "SELECTED"; ?>>ID</option> |
|
128 |
<option value="menu_order" <?php if($OrderBy=="menu_order") echo "SELECTED"; ?>>menu_order</option> |
|
129 |
<option value="mime_type" <?php if($OrderBy=="mime_type") echo "SELECTED"; ?>>mime_type</option> |
|
130 |
<option value="modified" <?php if($OrderBy=="modified") echo "SELECTED"; ?>>modified</option> |
|
131 |
<option value="name" <?php if($OrderBy=="name") echo "SELECTED"; ?>>name</option> |
|
132 |
<option value="parent" <?php if($OrderBy=="parent") echo "SELECTED"; ?>>parent</option> |
|
133 |
<option value="password" <?php if($OrderBy=="password") echo "SELECTED"; ?>>password</option> |
|
134 |
<option value="rand" <?php if($OrderBy=="rand") echo "SELECTED"; ?>>rand</option> |
|
135 |
<option value="status" <?php if($OrderBy=="status") echo "SELECTED"; ?>>status</option> |
|
136 |
<option value="title" <?php if($OrderBy=="title") echo "SELECTED"; ?>>title</option> |
|
137 |
<option value="type" <?php if($OrderBy=="type") echo "SELECTED"; ?>>type</option> |
|
138 |
</select> |
|
139 |
</p> |
|
140 |
<p> |
|
141 |
<label for="<?php echo $this->get_field_id('AscDesc'); ?>"> <?php _e('Sort Order By : ', 'posts-of-cur-cat');?></label> |
|
142 |
<select class="select" id="<?php echo $this->get_field_id('AscDesc'); ?>" name="<?php echo $this->get_field_name('AscDesc'); ?>"> |
|
143 |
<option value="ASC" <?php if($AscDesc=="ASC") echo "SELECTED"; ?>>ASCending</option> |
|
144 |
<option value="DESC" <?php if($AscDesc=="DESC") echo "SELECTED"; ?>>DESCending</option> |
|
145 |
</select> |
|
146 |
</p> |
|
147 |
<?php |
|
148 |
} //end of form |
|
149 |
} |
|
150 |
||
151 |
add_action( 'widgets_init', create_function('', 'return register_widget("posts_cur_cat");') ); |
|
152 |
//Register Widget |
|
153 |
||
154 |
// Show recent posts function |
|
155 |
function post_of_curCat($args = '') { |
|
156 |
global $wpdb; |
|
157 |
||
158 |
$defaults = array( |
|
159 |
'limit' => 10, |
|
160 |
'AscDesc' => "ASC", |
|
161 |
'OrderBy' =>'ID', |
|
162 |
||
163 |
); |
|
164 |
$args = wp_parse_args( $args, $defaults ); |
|
165 |
extract($args); |
|
166 |
||
167 |
$limit = (int) abs($limit); |
|
168 |
||
169 |
if(is_category()){ |
|
170 |
$curCategoryID = get_query_var('cat'); |
|
171 |
} |
|
172 |
if (is_single()) { |
|
173 |
$curCategoryID = ''; |
|
174 |
foreach (get_the_category() as $catt) { |
|
175 |
$curCategoryID .= $catt->cat_ID.' '; |
|
176 |
} |
|
177 |
$curCategoryID = str_replace(" ", ",", trim($curCategoryID)); |
|
178 |
} |
|
179 |
if (!intval($curCategoryID)) $curCategoryID=''; |
|
180 |
||
181 |
$query = "&category=$curCategoryID&showposts=$limit&orderby=$OrderBy&order=$AscDesc"; |
|
182 |
$posts = get_posts($query); //get posts |
|
183 |
$postlist = ''; |
|
184 |
foreach ($posts as $post) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
185 |
$post_title = htmlspecialchars(stripslashes($post->post_title)); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
186 |
$postlist .= '<li><a href="' . get_permalink($post->ID) . '" title="'. $post_title .'" >' . $post_title . '</a> </li>'; |
136 | 187 |
} |
188 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
189 |
echo '<ul class="posts-of-current-category">'; |
136 | 190 |
echo $postlist; |
191 |
echo '</ul>'; |
|
192 |
} |
|
193 |
?> |