Display Posts Shortcode - Version 2.4

Version Description

Download this release

Release Info

Developer billerickson
Plugin Icon 128x128 Display Posts Shortcode
Version 2.4
Comparing to
See all releases

Code changes from version 2.3 to 2.4

Files changed (2) hide show
  1. display-posts-shortcode.php +88 -17
  2. readme.txt +13 -3
display-posts-shortcode.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Display Posts Shortcode
4
  * Plugin URI: http://www.billerickson.net/shortcode-to-display-posts/
5
  * Description: Display a listing of posts using the [display-posts] shortcode
6
- * Version: 2.3
7
  * Author: Bill Erickson
8
  * Author URI: http://www.billerickson.net
9
  *
@@ -15,7 +15,7 @@
15
  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
  *
17
  * @package Display Posts
18
- * @version 2.2
19
  * @author Bill Erickson <bill@billerickson.net>
20
  * @copyright Copyright (c) 2011, Bill Erickson
21
  * @link http://www.billerickson.net/shortcode-to-display-posts/
@@ -49,16 +49,22 @@ function be_display_posts_shortcode( $atts ) {
49
 
50
  // Pull in shortcode attributes and set defaults
51
  $atts = shortcode_atts( array(
 
52
  'author' => '',
53
  'category' => '',
54
  'date_format' => '(n/j/Y)',
 
 
55
  'id' => false,
56
  'ignore_sticky_posts' => false,
57
  'image_size' => false,
 
 
58
  'include_content' => false,
59
  'include_date' => false,
60
  'include_excerpt' => false,
61
  'meta_key' => '',
 
62
  'no_posts_message' => '',
63
  'offset' => 0,
64
  'order' => 'DESC',
@@ -72,18 +78,29 @@ function be_display_posts_shortcode( $atts ) {
72
  'tax_term' => false,
73
  'taxonomy' => false,
74
  'wrapper' => 'ul',
75
- ), $atts );
 
 
 
 
 
 
76
 
 
77
  $author = sanitize_text_field( $atts['author'] );
78
  $category = sanitize_text_field( $atts['category'] );
79
  $date_format = sanitize_text_field( $atts['date_format'] );
 
80
  $id = $atts['id']; // Sanitized later as an array of integers
81
- $ignore_sticky_posts = (bool) $atts['ignore_sticky_posts'];
82
  $image_size = sanitize_key( $atts['image_size'] );
83
- $include_content = (bool)$atts['include_content'];
84
- $include_date = (bool)$atts['include_date'];
85
- $include_excerpt = (bool)$atts['include_excerpt'];
 
 
86
  $meta_key = sanitize_text_field( $atts['meta_key'] );
 
87
  $no_posts_message = sanitize_text_field( $atts['no_posts_message'] );
88
  $offset = intval( $atts['offset'] );
89
  $order = sanitize_key( $atts['order'] );
@@ -97,6 +114,12 @@ function be_display_posts_shortcode( $atts ) {
97
  $tax_term = sanitize_text_field( $atts['tax_term'] );
98
  $taxonomy = sanitize_key( $atts['taxonomy'] );
99
  $wrapper = sanitize_text_field( $atts['wrapper'] );
 
 
 
 
 
 
100
 
101
 
102
  // Set up initial query for post
@@ -117,12 +140,20 @@ function be_display_posts_shortcode( $atts ) {
117
  if( !empty( $meta_key ) )
118
  $args['meta_key'] = $meta_key;
119
 
 
 
 
 
120
  // If Post IDs
121
  if( $id ) {
122
  $posts_in = array_map( 'intval', explode( ',', $id ) );
123
  $args['post__in'] = $posts_in;
124
  }
125
 
 
 
 
 
126
  // Post Author
127
  if( !empty( $author ) )
128
  $args['author_name'] = $author;
@@ -203,7 +234,7 @@ function be_display_posts_shortcode( $atts ) {
203
  if( $post_parent ) {
204
  if( 'current' == $post_parent ) {
205
  global $post;
206
- $post_parent = $post->ID;
207
  }
208
  $args['post_parent'] = intval( $post_parent );
209
  }
@@ -223,37 +254,77 @@ function be_display_posts_shortcode( $atts ) {
223
  $inner = '';
224
  while ( $listing->have_posts() ): $listing->the_post(); global $post;
225
 
226
- $image = $date = $excerpt = $content = '';
227
 
228
- $title = '<a class="title" href="' . apply_filters( 'the_permalink', get_permalink() ) . '">' . apply_filters( 'the_title', get_the_title() ) . '</a>';
 
229
 
230
  if ( $image_size && has_post_thumbnail() )
231
- $image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( $post->ID, $image_size ) . '</a> ';
232
 
233
  if ( $include_date )
234
  $date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
 
 
 
235
 
236
  if ( $include_excerpt )
237
  $excerpt = ' <span class="excerpt-dash">-</span> <span class="excerpt">' . get_the_excerpt() . '</span>';
238
 
239
- if( $include_content )
 
240
  $content = '<div class="content">' . apply_filters( 'the_content', get_the_content() ) . '</div>';
 
 
241
 
242
  $class = array( 'listing-item' );
243
- $class = apply_filters( 'display_posts_shortcode_post_class', $class, $post, $listing );
244
- $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $excerpt . $content . '</' . $inner_wrapper . '>';
245
 
246
  // If post is set to private, only show to logged in users
247
- if( 'private' == get_post_status( $post->ID ) && !current_user_can( 'read_private_posts' ) )
248
  $output = '';
249
 
250
  $inner .= apply_filters( 'display_posts_shortcode_output', $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class );
251
 
252
  endwhile; wp_reset_postdata();
253
 
254
- $open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . ' class="display-posts-listing">', $original_atts );
255
  $close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>', $original_atts );
256
- $return = $open . $inner . $close;
 
 
 
 
 
 
 
 
 
 
257
 
258
  return $return;
259
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  * Plugin Name: Display Posts Shortcode
4
  * Plugin URI: http://www.billerickson.net/shortcode-to-display-posts/
5
  * Description: Display a listing of posts using the [display-posts] shortcode
6
+ * Version: 2.4
7
  * Author: Bill Erickson
8
  * Author URI: http://www.billerickson.net
9
  *
15
  * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
  *
17
  * @package Display Posts
18
+ * @version 2.4
19
  * @author Bill Erickson <bill@billerickson.net>
20
  * @copyright Copyright (c) 2011, Bill Erickson
21
  * @link http://www.billerickson.net/shortcode-to-display-posts/
49
 
50
  // Pull in shortcode attributes and set defaults
51
  $atts = shortcode_atts( array(
52
+ 'title' => '',
53
  'author' => '',
54
  'category' => '',
55
  'date_format' => '(n/j/Y)',
56
+ 'display_posts_off' => false,
57
+ 'exclude_current' => false,
58
  'id' => false,
59
  'ignore_sticky_posts' => false,
60
  'image_size' => false,
61
+ 'include_title' => true,
62
+ 'include_author' => false,
63
  'include_content' => false,
64
  'include_date' => false,
65
  'include_excerpt' => false,
66
  'meta_key' => '',
67
+ 'meta_value' => '',
68
  'no_posts_message' => '',
69
  'offset' => 0,
70
  'order' => 'DESC',
78
  'tax_term' => false,
79
  'taxonomy' => false,
80
  'wrapper' => 'ul',
81
+ 'wrapper_class' => 'display-posts-listing',
82
+ 'wrapper_id' => false,
83
+ ), $atts, 'display-posts' );
84
+
85
+ // End early if shortcode should be turned off
86
+ if( $atts['display_posts_off'] )
87
+ return;
88
 
89
+ $shortcode_title = sanitize_text_field( $atts['title'] );
90
  $author = sanitize_text_field( $atts['author'] );
91
  $category = sanitize_text_field( $atts['category'] );
92
  $date_format = sanitize_text_field( $atts['date_format'] );
93
+ $exclude_current = be_display_posts_bool( $atts['exclude_current'] );
94
  $id = $atts['id']; // Sanitized later as an array of integers
95
+ $ignore_sticky_posts = be_display_posts_bool( $atts['ignore_sticky_posts'] );
96
  $image_size = sanitize_key( $atts['image_size'] );
97
+ $include_title = be_display_posts_bool( $atts['include_title'] );
98
+ $include_author = be_display_posts_bool( $atts['include_author'] );
99
+ $include_content = be_display_posts_bool( $atts['include_content'] );
100
+ $include_date = be_display_posts_bool( $atts['include_date'] );
101
+ $include_excerpt = be_display_posts_bool( $atts['include_excerpt'] );
102
  $meta_key = sanitize_text_field( $atts['meta_key'] );
103
+ $meta_value = sanitize_text_field( $atts['meta_value'] );
104
  $no_posts_message = sanitize_text_field( $atts['no_posts_message'] );
105
  $offset = intval( $atts['offset'] );
106
  $order = sanitize_key( $atts['order'] );
114
  $tax_term = sanitize_text_field( $atts['tax_term'] );
115
  $taxonomy = sanitize_key( $atts['taxonomy'] );
116
  $wrapper = sanitize_text_field( $atts['wrapper'] );
117
+ $wrapper_class = sanitize_html_class( $atts['wrapper_class'] );
118
+ if( !empty( $wrapper_class ) )
119
+ $wrapper_class = ' class="' . $wrapper_class . '"';
120
+ $wrapper_id = sanitize_html_class( $atts['wrapper_id'] );
121
+ if( !empty( $wrapper_id ) )
122
+ $wrapper_id = ' id="' . $wrapper_id . '"';
123
 
124
 
125
  // Set up initial query for post
140
  if( !empty( $meta_key ) )
141
  $args['meta_key'] = $meta_key;
142
 
143
+ // Meta value (for simple meta queries)
144
+ if( !empty( $meta_value ) )
145
+ $args['meta_value'] = $meta_value;
146
+
147
  // If Post IDs
148
  if( $id ) {
149
  $posts_in = array_map( 'intval', explode( ',', $id ) );
150
  $args['post__in'] = $posts_in;
151
  }
152
 
153
+ // If Exclude Current
154
+ if( $exclude_current )
155
+ $args['post__not_in'] = array( get_the_ID() );
156
+
157
  // Post Author
158
  if( !empty( $author ) )
159
  $args['author_name'] = $author;
234
  if( $post_parent ) {
235
  if( 'current' == $post_parent ) {
236
  global $post;
237
+ $post_parent = get_the_ID();
238
  }
239
  $args['post_parent'] = intval( $post_parent );
240
  }
254
  $inner = '';
255
  while ( $listing->have_posts() ): $listing->the_post(); global $post;
256
 
257
+ $image = $date = $author = $excerpt = $content = '';
258
 
259
+ if ( $include_title )
260
+ $title = '<a class="title" href="' . apply_filters( 'the_permalink', get_permalink() ) . '">' . get_the_title() . '</a>';
261
 
262
  if ( $image_size && has_post_thumbnail() )
263
+ $image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</a> ';
264
 
265
  if ( $include_date )
266
  $date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
267
+
268
+ if( $include_author )
269
+ $author = apply_filters( 'display_posts_shortcode_author', ' <span class="author">by ' . get_the_author() . '</span>' );
270
 
271
  if ( $include_excerpt )
272
  $excerpt = ' <span class="excerpt-dash">-</span> <span class="excerpt">' . get_the_excerpt() . '</span>';
273
 
274
+ if( $include_content ) {
275
+ add_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
276
  $content = '<div class="content">' . apply_filters( 'the_content', get_the_content() ) . '</div>';
277
+ remove_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
278
+ }
279
 
280
  $class = array( 'listing-item' );
281
+ $class = sanitize_html_class( apply_filters( 'display_posts_shortcode_post_class', $class, $post, $listing, $original_atts ) );
282
+ $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $excerpt . $content . '</' . $inner_wrapper . '>';
283
 
284
  // If post is set to private, only show to logged in users
285
+ if( 'private' == get_post_status( get_the_ID() ) && !current_user_can( 'read_private_posts' ) )
286
  $output = '';
287
 
288
  $inner .= apply_filters( 'display_posts_shortcode_output', $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class );
289
 
290
  endwhile; wp_reset_postdata();
291
 
292
+ $open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . $wrapper_class . $wrapper_id . '>', $original_atts );
293
  $close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>', $original_atts );
294
+
295
+ $return = $open;
296
+
297
+ if( $shortcode_title ) {
298
+
299
+ $title_tag = apply_filters( 'display_posts_shortcode_title_tag', 'h2', $original_atts );
300
+
301
+ $return .= '<' . $title_tag . ' class="display-posts-title">' . $shortcode_title . '</' . $title_tag . '>' . "\n";
302
+ }
303
+
304
+ $return .= $inner . $close;
305
 
306
  return $return;
307
  }
308
+
309
+ /**
310
+ * Turn off display posts shortcode
311
+ * If display full post content, any uses of [display-posts] are disabled
312
+ *
313
+ * @param array $out, returned shortcode values
314
+ * @param array $pairs, list of supported attributes and their defaults
315
+ * @param array $atts, original shortcode attributes
316
+ * @return array $out
317
+ */
318
+ function be_display_posts_off( $out, $pairs, $atts ) {
319
+ $out['display_posts_off'] = true;
320
+ return $out;
321
+ }
322
+
323
+ /**
324
+ * Convert string to boolean
325
+ * because (bool) "false" == true
326
+ *
327
+ */
328
+ function be_display_posts_bool( $value ) {
329
+ return !empty( $value ) && 'true' == $value ? true : false;
330
+ }
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: billerickson
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MQKRBRFVRUV8C
4
  Tags: shortcode, pages, posts, page, query, display, list
5
  Requires at least: 3.0
6
- Tested up to: 3.5
7
- Stable tag: 2.3
8
 
9
  Display a listing of posts using the [display-posts] shortcode
10
 
@@ -16,7 +16,9 @@ Add the shortcode in a post or page, and use the arguments to query based on tag
16
 
17
  See the [WordPress Codex](http://codex.wordpress.org/Class_Reference/WP_Query) for information on using the arguments.
18
 
19
- [Documentation](https://github.com/billerickson/display-posts-shortcode/wiki) | [Support Forum](https://github.com/billerickson/display-posts-shortcode/issues)
 
 
20
 
21
  == Installation ==
22
 
@@ -27,7 +29,15 @@ See the [WordPress Codex](http://codex.wordpress.org/Class_Reference/WP_Query) f
27
 
28
  == Changelog ==
29
 
 
 
 
 
 
 
 
30
  **Version 2.3**
 
31
  * Include the shortcode attributes on wrapper filter
32
  * Add 'no_posts_message' parameter to specify content displayed if no posts found
33
  * Add filters to the title and permalink
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MQKRBRFVRUV8C
4
  Tags: shortcode, pages, posts, page, query, display, list
5
  Requires at least: 3.0
6
+ Tested up to: 3.9
7
+ Stable tag: 2.4
8
 
9
  Display a listing of posts using the [display-posts] shortcode
10
 
16
 
17
  See the [WordPress Codex](http://codex.wordpress.org/Class_Reference/WP_Query) for information on using the arguments.
18
 
19
+ [Documentation](https://github.com/billerickson/display-posts-shortcode/wiki)
20
+
21
+ **No support will be provided by the developer**
22
 
23
  == Installation ==
24
 
29
 
30
  == Changelog ==
31
 
32
+ **Version 2.4**
33
+
34
+ * Add 'include_author' parameter
35
+ * Add 'exclude_current' parameter for excluding the current post from the results
36
+ * If you display the full content of results, additional uses of the shortcode within those posts are now turned off
37
+ * Other minor improvements
38
+
39
  **Version 2.3**
40
+
41
  * Include the shortcode attributes on wrapper filter
42
  * Add 'no_posts_message' parameter to specify content displayed if no posts found
43
  * Add filters to the title and permalink