List category posts - Version 0.18

Version Description

Template system was upgraded with new options. Backwards compatible, but you can better customize the way the post contents are displayed. Check templates/default.php.

Download this release

Release Info

Developer fernandobt
Plugin Icon 128x128 List category posts
Version 0.18
Comparing to
See all releases

Code changes from version 0.17.2 to 0.18

include/CatList.php CHANGED
@@ -9,7 +9,6 @@ class CatList{
9
  private $params = array();
10
  private $lcp_categories_posts = array();
11
  private $lcp_category_id = 0;
12
- private $lcp_category_name = '';
13
 
14
  /**
15
  * Constructor gets the shortcode attributes as parameter
@@ -26,13 +25,12 @@ class CatList{
26
  */
27
  private function lcp_set_categories(){
28
  if($this->params['name'] != '' && $this->params['id'] == '0'){
29
- $this->lcp_category_name = $this->params['name'];
30
- $lcp_category = 'category_name=' . $this->lcp_category_name;
31
- //$this->lcp_category_id = ;
32
  }else{
33
  $this->lcp_category_id = $this->params['id'];
34
- $lcp_category = 'cat=' . $this->lcp_category_id;
35
  }
 
 
36
 
37
  //Build the query for get_posts()
38
  $lcp_query = $lcp_category.'&numberposts=' . $this->params['numberposts'] .
@@ -53,17 +51,34 @@ class CatList{
53
  $this->lcp_categories_posts = get_posts($lcp_query);
54
  }
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  public function get_categories_posts(){
57
  return $this->lcp_categories_posts;
58
  }
59
-
60
  /**
61
  * Load category name and link to the category:
62
  */
63
  public function get_category_link(){
64
- $cat_link = get_category_link($this->lcp_category_id);
65
- $cat_title = get_cat_name($this->lcp_category_id);
66
- return '<a href="' . $cat_link . '" title="' . $cat_title . '">' . $cat_title . '</a>';
 
 
 
 
67
  }
68
 
69
  /**
@@ -91,12 +106,16 @@ class CatList{
91
  endif;
92
  }
93
  return $lcp_customs;
 
 
94
  }
95
  }
96
 
97
  public function get_comments_count($single){
98
  if ($this->params['comments'] == 'yes'){
99
  return ' (' . $single->comment_count . ')';
 
 
100
  }
101
  }
102
 
@@ -104,6 +123,8 @@ class CatList{
104
  if ($this->params['author']=='yes'){
105
  $lcp_userdata = get_userdata($single->post_author);
106
  return $lcp_userdata->display_name;
 
 
107
  }
108
  }
109
 
@@ -113,6 +134,8 @@ class CatList{
113
  if ($this->params['date']=='yes'){
114
  //by Verex, great idea!
115
  return get_the_time($this->params['dateformat'], $single);
 
 
116
  }
117
  }
118
 
@@ -129,7 +152,9 @@ class CatList{
129
  $lcp_content = apply_filters('the_content', $lcp_content); // added to parse shortcodes
130
  $lcp_content = str_replace(']]>', ']]&gt', $lcp_content); // added to parse shortcodes
131
  return $lcp_content;
132
- }
 
 
133
  }
134
 
135
  public function get_excerpt($single){
@@ -146,6 +171,8 @@ class CatList{
146
  $lcp_excerpt = substr($lcp_excerpt, 0, 252) . '...';
147
  }
148
  return $lcp_excerpt;
 
 
149
  }
150
  }
151
 
@@ -161,6 +188,8 @@ class CatList{
161
  $lcp_thumbnail = get_the_post_thumbnail($single->ID);
162
  }
163
  return $lcp_thumbnail;
 
 
164
  }
165
  }
166
 
9
  private $params = array();
10
  private $lcp_categories_posts = array();
11
  private $lcp_category_id = 0;
 
12
 
13
  /**
14
  * Constructor gets the shortcode attributes as parameter
25
  */
26
  private function lcp_set_categories(){
27
  if($this->params['name'] != '' && $this->params['id'] == '0'){
28
+ $this->lcp_category_id = $this->get_category_id_by_name($this->params['name']);
 
 
29
  }else{
30
  $this->lcp_category_id = $this->params['id'];
 
31
  }
32
+
33
+ $lcp_category = 'cat=' . $this->lcp_category_id;
34
 
35
  //Build the query for get_posts()
36
  $lcp_query = $lcp_category.'&numberposts=' . $this->params['numberposts'] .
51
  $this->lcp_categories_posts = get_posts($lcp_query);
52
  }
53
 
54
+ /**
55
+ * Get the category id from its name
56
+ * by Eric Celeste / http://eric.clst.org
57
+ */
58
+ private function get_category_id_by_name($cat_name){
59
+ $term = get_term_by('name', $cat_name, 'category');
60
+ return $term->term_id;
61
+ }
62
+
63
+ public function get_category_id(){
64
+ return $this->lcp_category_id;
65
+ }
66
+
67
  public function get_categories_posts(){
68
  return $this->lcp_categories_posts;
69
  }
70
+
71
  /**
72
  * Load category name and link to the category:
73
  */
74
  public function get_category_link(){
75
+ if($this->params['customfield_display'] != ''){
76
+ $cat_link = get_category_link($this->lcp_category_id);
77
+ $cat_title = get_cat_name($this->lcp_category_id);
78
+ return '<a href="' . $cat_link . '" title="' . $cat_title . '">' . $cat_title . '</a>';
79
+ } else {
80
+ return null;
81
+ }
82
  }
83
 
84
  /**
106
  endif;
107
  }
108
  return $lcp_customs;
109
+ } else {
110
+ return null;
111
  }
112
  }
113
 
114
  public function get_comments_count($single){
115
  if ($this->params['comments'] == 'yes'){
116
  return ' (' . $single->comment_count . ')';
117
+ } else {
118
+ return null;
119
  }
120
  }
121
 
123
  if ($this->params['author']=='yes'){
124
  $lcp_userdata = get_userdata($single->post_author);
125
  return $lcp_userdata->display_name;
126
+ } else {
127
+ return null;
128
  }
129
  }
130
 
134
  if ($this->params['date']=='yes'){
135
  //by Verex, great idea!
136
  return get_the_time($this->params['dateformat'], $single);
137
+ } else {
138
+ return null;
139
  }
140
  }
141
 
152
  $lcp_content = apply_filters('the_content', $lcp_content); // added to parse shortcodes
153
  $lcp_content = str_replace(']]>', ']]&gt', $lcp_content); // added to parse shortcodes
154
  return $lcp_content;
155
+ } else {
156
+ return null;
157
+ }
158
  }
159
 
160
  public function get_excerpt($single){
171
  $lcp_excerpt = substr($lcp_excerpt, 0, 252) . '...';
172
  }
173
  return $lcp_excerpt;
174
+ } else {
175
+ return null;
176
  }
177
  }
178
 
188
  $lcp_thumbnail = get_the_post_thumbnail($single->ID);
189
  }
190
  return $lcp_thumbnail;
191
+ } else {
192
+ return null;
193
  }
194
  }
195
 
include/CatListDisplayer.php CHANGED
@@ -55,6 +55,7 @@ class CatListDisplayer {
55
  }
56
 
57
  private function build_output($tag){
 
58
  $this->lcp_output .= '<' . $tag . ' class="'.$this->params['class'].'">';
59
  $inner_tag = ($tag == 'ul') ? 'li' : 'p';
60
  //Posts loop
@@ -84,9 +85,9 @@ class CatListDisplayer {
84
 
85
  $lcp_display_output .= $this->get_thumbnail($single);
86
 
87
- $lcp_display_output .= $this->get_content($single);
88
 
89
- $lcp_display_output .= $this->get_excerpt($single);
90
 
91
  $lcp_display_output .= '</' . $tag . '>';
92
 
@@ -96,38 +97,70 @@ class CatListDisplayer {
96
  /**
97
  * Auxiliary functions for templates
98
  */
99
- private function get_author($single){
100
- return $this->catlist->get_author_to_show($single);
 
101
  }
102
 
103
- private function get_comments($single){
104
- return $this->catlist->get_comments_count($single);
 
105
  }
106
 
107
- private function get_content($single){
108
- return $this->catlist->get_content($single);
 
109
  }
110
 
111
- private function get_custom_fields($custom_key, $post_id){
112
- return $this->catlist->get_custom_fields($custom_key, $post_id);
 
113
  }
114
 
115
- private function get_date($single){
116
- return $this->catlist->get_date_to_show($single);
 
117
  }
118
 
119
- private function get_excerpt($single){
120
- return $this->catlist->get_excerpt($single);
 
121
  }
122
 
123
- private function get_thumbnail($single){
124
- return $this->catlist->get_thumbnail($single);
 
125
  }
126
 
127
- private function get_post_title($single){
128
  return '<a href="' . get_permalink($single->ID).'">' . $single->post_title . '</a>';
129
  }
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
 
133
  }
55
  }
56
 
57
  private function build_output($tag){
58
+ $this->lcp_output .= $this->get_category_link($single, 'strong');
59
  $this->lcp_output .= '<' . $tag . ' class="'.$this->params['class'].'">';
60
  $inner_tag = ($tag == 'ul') ? 'li' : 'p';
61
  //Posts loop
85
 
86
  $lcp_display_output .= $this->get_thumbnail($single);
87
 
88
+ $lcp_display_output .= $this->get_content($single, 'p');
89
 
90
+ $lcp_display_output .= $this->get_excerpt($single, 'p', 'lcp_excerpt');
91
 
92
  $lcp_display_output .= '</' . $tag . '>';
93
 
97
  /**
98
  * Auxiliary functions for templates
99
  */
100
+ private function get_author($single, $tag = null, $css_class = null){
101
+ $info = $this->catlist->get_author_to_show($single);
102
+ return $this->assign_style($info, $tag, $css_class);
103
  }
104
 
105
+ private function get_comments($single, $tag = null, $class = null){
106
+ $info = $this->catlist->get_comments_count($single);
107
+ return $this->assign_style($info, $tag, $css_class);
108
  }
109
 
110
+ private function get_content($single, $tag = null, $css_class = null){
111
+ $info = $this->catlist->get_content($single);
112
+ return $this->assign_style($info, $tag, $css_class);
113
  }
114
 
115
+ private function get_custom_fields($custom_key, $post_id, $tag = null, $css_class = null){
116
+ $info = $this->catlist->get_custom_fields($custom_key, $post_id);
117
+ return $this->assign_style($info, $tag, $css_class);
118
  }
119
 
120
+ private function get_date($single, $tag = null, $css_class = null){
121
+ $info = $this->catlist->get_date_to_show($single);
122
+ return $this->assign_style($info, $tag, $css_class);
123
  }
124
 
125
+ private function get_excerpt($single, $tag = null, $css_class = null){
126
+ $info = $this->catlist->get_excerpt($single);
127
+ return $this->assign_style($info, $tag, $css_class);
128
  }
129
 
130
+ private function get_thumbnail($single, $tag = null, $css_class = null){
131
+ $info = $this->catlist->get_thumbnail($single);
132
+ return $this->assign_style($info, $tag, $css_class);
133
  }
134
 
135
+ private function get_post_title($single, $tag = null, $css_class = null){
136
  return '<a href="' . get_permalink($single->ID).'">' . $single->post_title . '</a>';
137
  }
138
 
139
+ private function get_category_link($single, $tag = null, $css_class = null){
140
+ $info = $this->catlist->get_category_link();
141
+ return $this->assign_style($info, $tag, $css_class);
142
+ }
143
+
144
+ /**
145
+ * Assign style to the info delivered by CatList. Tag is an HTML tag
146
+ * which is passed and will sorround the info. Css_class is the css
147
+ * class we want to assign to this tag.
148
+ * @param string $info
149
+ * @param string $tag
150
+ * @param string $css_class
151
+ * @return string
152
+ */
153
+ private function assign_style($info, $tag = null, $css_class = null){
154
+ if (!is_null($info)){
155
+ if (is_null($tag)){
156
+ return $info;
157
+ } elseif (!is_null($tag) && is_null($css_class)) {
158
+ return '<' . $tag . '>' . $info . '</' . $tag . '>';
159
+ }
160
+ return '<' . $tag . ' class="' . $css_class . '">' . $info . '</' . $tag . '>';
161
+ }
162
+ }
163
+
164
 
165
 
166
  }
list_cat_posts.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: List category posts
4
  Plugin URI: http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english/
5
  Description: List Category Posts allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments. Usage: [catlist argument1=value1 argument2=value2].
6
- Version: 0.17.2
7
  Author: Fernando Briano
8
  Author URI: http://picandocodigo.net/
9
  */
@@ -71,3 +71,11 @@ class ListCategoryPosts{
71
  }
72
 
73
  add_shortcode( 'catlist', array('ListCategoryPosts', 'catlist_func') );
 
 
 
 
 
 
 
 
3
  Plugin Name: List category posts
4
  Plugin URI: http://picandocodigo.net/programacion/wordpress/list-category-posts-wordpress-plugin-english/
5
  Description: List Category Posts allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments. Usage: [catlist argument1=value1 argument2=value2].
6
+ Version: 0.18
7
  Author: Fernando Briano
8
  Author URI: http://picandocodigo.net/
9
  */
71
  }
72
 
73
  add_shortcode( 'catlist', array('ListCategoryPosts', 'catlist_func') );
74
+
75
+ /**
76
+ * TO-DO:
77
+ * - i18n
78
+ * - Pagination
79
+ * - Simpler template system
80
+ * - Exclude child categories
81
+ */
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate Link: http://picandocodigo.net/programacion/wordpress/list-category-posts
4
  Tags: list, categories, posts, cms
5
  Requires at least: 2.8
6
  Tested up to: 3.1
7
- Stable tag: 0.17.2
8
 
9
  == Description ==
10
  List Category Posts is a simple WordPress plugin which allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments.
@@ -107,8 +107,19 @@ Your comments and feedback are welcome at: http://foro.picandocodigo.net/categor
107
  * **New feature requests** - Contact me on fernando at picandocodigo dot net or check out the forum.
108
  * **Support** I've decided to use WordPress Answers (http://meta.wordpress.stackexchange.com/) as the place for support. It's a great place with a large community of WordPress users and developers. Just ask your question with the tag 'plugin-list-category-post'.
109
 
 
 
 
 
 
 
 
 
110
  == Upgrade Notice ==
111
 
 
 
 
112
  = 0.17 =
113
  Upgrade your templates: Templates system was rewritten, so your current templates will probably not work. Check out the new default.php file on /templates to see the simpler new way to work with templates.
114
 
@@ -126,6 +137,11 @@ Template system has changed. Custom templates should be stored in WordPress them
126
 
127
  == Changelog ==
128
 
 
 
 
 
 
129
  = 0.17.1 =
130
  * Fixed displaying of "Author:" even when not being called.
131
 
4
  Tags: list, categories, posts, cms
5
  Requires at least: 2.8
6
  Tested up to: 3.1
7
+ Stable tag: 0.18
8
 
9
  == Description ==
10
  List Category Posts is a simple WordPress plugin which allows you to list posts from a category into a post/page using the [catlist] shortcode. This shortcode accepts a category name or id, the order in which you want the posts to display, and the number of posts to display. You can use [catlist] as many times as needed with different arguments.
107
  * **New feature requests** - Contact me on fernando at picandocodigo dot net or check out the forum.
108
  * **Support** I've decided to use WordPress Answers (http://meta.wordpress.stackexchange.com/) as the place for support. It's a great place with a large community of WordPress users and developers. Just ask your question with the tag 'plugin-list-category-post'.
109
 
110
+ * **FAQ**
111
+
112
+ Plugin could not be activated because it triggered a fatal error.
113
+ Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /.../wp-content/plugins/list-category-posts/include/CatListDisplayer.php on line 10
114
+
115
+ Please check:
116
+ http://wordpress.stackexchange.com/questions/9338/list-category-posts-plugin-upgrade-fails-fatal-error/9340#9340
117
+
118
  == Upgrade Notice ==
119
 
120
+ = 0.18 =
121
+ Template system was upgraded with new options. Backwards compatible, but you can better customize the way the post contents are displayed. Check templates/default.php.
122
+
123
  = 0.17 =
124
  Upgrade your templates: Templates system was rewritten, so your current templates will probably not work. Check out the new default.php file on /templates to see the simpler new way to work with templates.
125
 
137
 
138
  == Changelog ==
139
 
140
+ = 0.18 =
141
+ * Fixed category id bug. Reported and fixed by Eric Celeste / http://eric.clst.org, thanks!
142
+ * Improved template system a liitle bit, now you can pass an HTML tag and a CSS class to sorround each field on your template.
143
+ * Added category link which wasn't working after previous big update.
144
+
145
  = 0.17.1 =
146
  * Fixed displaying of "Author:" even when not being called.
147
 
templates/default.php CHANGED
@@ -31,10 +31,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
  * the instance of CatListDisplayer that called this file.
32
  */
33
 
34
- /* This is the string which will gather all the information.
35
- * We're starting it */
36
  $lcp_display_output = '';
37
 
 
 
 
38
  //Add 'starting' tag. Here, I'm using an unordered list (ul) as an example:
39
  $lcp_output .= '<ul class="lcp_catlist">';
40
 
@@ -42,7 +44,12 @@ $lcp_output .= '<ul class="lcp_catlist">';
42
  * Posts loop.
43
  * The code here will be executed for every post in the category.
44
  * As you can see, the different options are being called from functions on the
45
- * $this variable which is a CatListDisplayer. The CatListDisplayer
 
 
 
 
 
46
  */
47
  foreach ($this->catlist->get_categories_posts() as $single):
48
  //Start a List Item for each post:
@@ -66,17 +73,21 @@ foreach ($this->catlist->get_categories_posts() as $single):
66
  //Post Thumbnail
67
  $lcp_display_output .= $this->get_thumbnail($single);
68
 
69
- //Post content
70
- $lcp_display_output .= $this->get_content($single);
 
 
 
71
 
72
- //Post excerpt
73
- $lcp_display_output .= $this->get_excerpt($single);
 
 
 
74
 
75
  //Close li tag
76
  $lcp_display_output .= '</li>';
77
  endforeach;
78
 
79
  $lcp_display_output .= '</ul>';
80
- $this->lcp_output = $lcp_display_output;
81
-
82
- ?>
31
  * the instance of CatListDisplayer that called this file.
32
  */
33
 
34
+ /* This is the string which will gather all the information.*/
 
35
  $lcp_display_output = '';
36
 
37
+ // Show category link:
38
+ $lcp_display_output .= $this->get_category_link($single, 'strong');
39
+
40
  //Add 'starting' tag. Here, I'm using an unordered list (ul) as an example:
41
  $lcp_output .= '<ul class="lcp_catlist">';
42
 
44
  * Posts loop.
45
  * The code here will be executed for every post in the category.
46
  * As you can see, the different options are being called from functions on the
47
+ * $this variable which is a CatListDisplayer.
48
+ *
49
+ * The CatListDisplayer has a function for each field we want to show.
50
+ * So you'll see get_excerpt, get_thumbnail, etc.
51
+ * You can now pass an html tag as a parameter. This tag will sorround the info
52
+ * you want to display. You can also assign a specific CSS class to each field.
53
  */
54
  foreach ($this->catlist->get_categories_posts() as $single):
55
  //Start a List Item for each post:
73
  //Post Thumbnail
74
  $lcp_display_output .= $this->get_thumbnail($single);
75
 
76
+ /**
77
+ * Post content - Example of how to use tag and class parameters:
78
+ * This will produce:<p class="lcp_content">The content</p>
79
+ */
80
+ $lcp_display_output .= $this->get_content($single, 'p', 'lcp_content');
81
 
82
+ /**
83
+ * Post content - Example of how to use tag and class parameters:
84
+ * This will produce:<div class="lcp_excerpt">The content</div>
85
+ */
86
+ $lcp_display_output .= $this->get_excerpt($single, 'div', 'lcp_expert');
87
 
88
  //Close li tag
89
  $lcp_display_output .= '</li>';
90
  endforeach;
91
 
92
  $lcp_display_output .= '</ul>';
93
+ $this->lcp_output = $lcp_display_output;