Display Posts Shortcode - Version 1.9

Version Description

Download this release

Release Info

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

Code changes from version 1.8 to 1.9

Files changed (2) hide show
  1. display-posts-shortcode.php +41 -41
  2. readme.txt +7 -1
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: 1.8
7
  * Author: Bill Erickson
8
  * Author URI: http://www.billerickson.net
9
  *
@@ -41,36 +41,37 @@
41
  */
42
 
43
  // Create the shortcode
44
- add_shortcode('display-posts', 'be_display_posts_shortcode');
45
- function be_display_posts_shortcode($atts) {
46
 
47
  // Pull in shortcode attributes and set defaults
48
  extract( shortcode_atts( array(
49
- 'post_type' => 'post',
50
- 'post_parent' => false,
51
- 'id' => false,
52
- 'tag' => '',
53
- 'category' => '',
54
- 'posts_per_page' => '10',
55
- 'order' => 'DESC',
56
- 'orderby' => 'date',
57
- 'include_date' => false,
58
  'include_excerpt' => false,
59
- 'image_size' => false,
60
- 'wrapper' => 'ul',
61
- 'taxonomy' => false,
62
- 'tax_term' => false,
63
- 'tax_operator' => 'IN'
 
 
 
 
 
64
  ), $atts ) );
65
 
66
  // Set up initial query for post
67
  $args = array(
68
- 'post_type' => explode( ',', $post_type ),
69
- 'tag' => $tag,
70
- 'category_name' => $category,
 
71
  'posts_per_page' => $posts_per_page,
72
- 'order' => $order,
73
- 'orderby' => $orderby,
74
  );
75
 
76
  // If Post IDs
@@ -94,8 +95,8 @@ function be_display_posts_shortcode($atts) {
94
  'tax_query' => array(
95
  array(
96
  'taxonomy' => $taxonomy,
97
- 'field' => 'slug',
98
- 'terms' => $tax_term,
99
  'operator' => $tax_operator
100
  )
101
  )
@@ -115,41 +116,40 @@ function be_display_posts_shortcode($atts) {
115
  // Set up html elements used to wrap the posts.
116
  // Default is ul/li, but can also be ol/li and div/div
117
  $wrapper_options = array( 'ul', 'ol', 'div' );
118
- if( !in_array( $wrapper, $wrapper_options ) )
119
  $wrapper = 'ul';
120
- if( 'div' == $wrapper )
121
- $inner_wrapper = 'div';
122
- else
123
- $inner_wrapper = 'li';
124
 
125
 
126
  $listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $atts ) );
127
- if ( !$listing->have_posts() )
128
- return apply_filters ('display_posts_shortcode_no_results', false );
129
 
130
  $inner = '';
131
  while ( $listing->have_posts() ): $listing->the_post(); global $post;
132
 
133
- if ( $image_size && has_post_thumbnail() ) $image = '<a class="image" href="'. get_permalink() .'">'. get_the_post_thumbnail($post->ID, $image_size).'</a> ';
134
- else $image = '';
135
-
136
- $title = '<a class="title" href="'. get_permalink() .'">'. get_the_title() .'</a>';
137
 
138
- if ($include_date) $date = ' <span class="date">('. get_the_date('n/j/Y') .')</span>';
139
- else $date = '';
 
 
 
140
 
141
- if ($include_excerpt) $excerpt = ' - <span class="excerpt">' . get_the_excerpt() . '</span>';
142
- else $excerpt = '';
143
 
144
  $output = '<' . $inner_wrapper . ' class="listing-item">' . $image . $title . $date . $excerpt . '</' . $inner_wrapper . '>';
145
 
146
  $inner .= apply_filters( 'display_posts_shortcode_output', $output, $atts, $image, $title, $date, $excerpt, $inner_wrapper );
147
 
148
- endwhile; wp_reset_query();
149
 
150
  $open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . ' class="display-posts-listing">' );
151
  $close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>' );
152
  $return = $open . $inner . $close;
153
 
154
  return $return;
155
- }
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: 1.9
7
  * Author: Bill Erickson
8
  * Author URI: http://www.billerickson.net
9
  *
41
  */
42
 
43
  // Create the shortcode
44
+ add_shortcode( 'display-posts', 'be_display_posts_shortcode' );
45
+ function be_display_posts_shortcode( $atts ) {
46
 
47
  // Pull in shortcode attributes and set defaults
48
  extract( shortcode_atts( array(
49
+ 'category' => '',
50
+ 'date_format' => '(n/j/Y)',
51
+ 'id' => false,
52
+ 'image_size' => false,
53
+ 'include_date' => false,
 
 
 
 
54
  'include_excerpt' => false,
55
+ 'order' => 'DESC',
56
+ 'orderby' => 'date',
57
+ 'post_parent' => false,
58
+ 'post_type' => 'post',
59
+ 'posts_per_page' => '10',
60
+ 'tag' => '',
61
+ 'tax_operator' => 'IN',
62
+ 'tax_term' => false,
63
+ 'taxonomy' => false,
64
+ 'wrapper' => 'ul',
65
  ), $atts ) );
66
 
67
  // Set up initial query for post
68
  $args = array(
69
+ 'category_name' => $category,
70
+ 'order' => $order,
71
+ 'orderby' => $orderby,
72
+ 'post_type' => explode( ',', $post_type ),
73
  'posts_per_page' => $posts_per_page,
74
+ 'tag' => $tag,
 
75
  );
76
 
77
  // If Post IDs
95
  'tax_query' => array(
96
  array(
97
  'taxonomy' => $taxonomy,
98
+ 'field' => 'slug',
99
+ 'terms' => $tax_term,
100
  'operator' => $tax_operator
101
  )
102
  )
116
  // Set up html elements used to wrap the posts.
117
  // Default is ul/li, but can also be ol/li and div/div
118
  $wrapper_options = array( 'ul', 'ol', 'div' );
119
+ if( ! in_array( $wrapper, $wrapper_options ) )
120
  $wrapper = 'ul';
121
+ $inner_wrapper = 'div' == $wrapper ? 'div' : 'li';
 
 
 
122
 
123
 
124
  $listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $atts ) );
125
+ if ( ! $listing->have_posts() )
126
+ return apply_filters( 'display_posts_shortcode_no_results', false );
127
 
128
  $inner = '';
129
  while ( $listing->have_posts() ): $listing->the_post(); global $post;
130
 
131
+ $image = $date = $excerpt = '';
132
+
133
+ $title = '<a class="title" href="' . get_permalink() . '">' . get_the_title() . '</a>';
 
134
 
135
+ if ( $image_size && has_post_thumbnail() )
136
+ $image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( $post->ID, $image_size ) . '</a> ';
137
+
138
+ if ( $include_date )
139
+ $date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
140
 
141
+ if ( $include_excerpt )
142
+ $excerpt = ' <span class="excerpt-dash">-</span> <span class="excerpt">' . get_the_excerpt() . '</span>';
143
 
144
  $output = '<' . $inner_wrapper . ' class="listing-item">' . $image . $title . $date . $excerpt . '</' . $inner_wrapper . '>';
145
 
146
  $inner .= apply_filters( 'display_posts_shortcode_output', $output, $atts, $image, $title, $date, $excerpt, $inner_wrapper );
147
 
148
+ endwhile; wp_reset_postdata();
149
 
150
  $open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . ' class="display-posts-listing">' );
151
  $close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>' );
152
  $return = $open . $inner . $close;
153
 
154
  return $return;
155
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: shortcode, pages, posts, page, query, display, list
5
  Requires at least: 3.0
6
  Tested up to: 3.4.1
7
- Stable tag: 1.8
8
 
9
  Display a listing of posts using the [display-posts] shortcode
10
 
@@ -27,6 +27,12 @@ See the [WordPress Codex](http://codex.wordpress.org/Class_Reference/WP_Query) f
27
 
28
  == Changelog ==
29
 
 
 
 
 
 
 
30
  **Version 1.8**
31
 
32
  * Added `display_posts_shortcode_no_results` filter for displaying content if there's no posts matching current query.
4
  Tags: shortcode, pages, posts, page, query, display, list
5
  Requires at least: 3.0
6
  Tested up to: 3.4.1
7
+ Stable tag: 1.9
8
 
9
  Display a listing of posts using the [display-posts] shortcode
10
 
27
 
28
  == Changelog ==
29
 
30
+ **Version 1.9**
31
+
32
+ * Add 'date_format' parameter, so you can customize how dates are displayed
33
+ * Added a class of .excerpt-dash so CSS can be used to remove the dash
34
+ * Cleaned up the codebase according to WordPress coding standards
35
+
36
  **Version 1.8**
37
 
38
  * Added `display_posts_shortcode_no_results` filter for displaying content if there's no posts matching current query.