Display Posts Shortcode - Version 1.7

Version Description

Download this release

Release Info

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

Version 1.7

Files changed (2) hide show
  1. display-posts-shortcode.php +156 -0
  2. readme.txt +121 -0
display-posts-shortcode.php ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
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.7
7
+ * Author: Bill Erickson
8
+ * Author URI: http://www.billerickson.net
9
+ *
10
+ * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
11
+ * General Public License version 2, as published by the Free Software Foundation. You may NOT assume
12
+ * that you can use any other version of the GPL.
13
+ *
14
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
15
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
+ *
17
+ * @package Display Posts
18
+ * @version 1.7
19
+ * @author Bill Erickson <bill@billerickson.net>
20
+ * @copyright Copyright (c) 2011, Bill Erickson
21
+ * @link http://www.billerickson.net/shortcode-to-display-posts/
22
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23
+ */
24
+
25
+
26
+ /**
27
+ * To Customize, use the following filters:
28
+ *
29
+ * `display_posts_shortcode_args`
30
+ * For customizing the $args passed to WP_Query
31
+ *
32
+ * `display_posts_shortcode_output`
33
+ * For customizing the output of individual posts.
34
+ * Example: https://gist.github.com/1175575#file_display_posts_shortcode_output.php
35
+ *
36
+ * `display_posts_shortcode_wrapper_open`
37
+ * display_posts_shortcode_wrapper_close`
38
+ * For customizing the outer markup of the whole listing. By default it is a <ul> but
39
+ * can be changed to <ol> or <div> using the 'wrapper' attribute, or by using this filter.
40
+ * Example: https://gist.github.com/1270278
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' => $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
77
+ if( $id ) {
78
+ $posts_in = explode( ',', $id );
79
+ $args['post__in'] = $posts_in;
80
+ }
81
+
82
+
83
+ // If taxonomy attributes, create a taxonomy query
84
+ if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
85
+
86
+ // Term string to array
87
+ $tax_term = explode( ', ', $tax_term );
88
+
89
+ // Validate operator
90
+ if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
91
+ $tax_operator = 'IN';
92
+
93
+ $tax_args = array(
94
+ 'tax_query' => array(
95
+ array(
96
+ 'taxonomy' => $taxonomy,
97
+ 'field' => 'slug',
98
+ 'terms' => $tax_term,
99
+ 'operator' => $tax_operator
100
+ )
101
+ )
102
+ );
103
+ $args = array_merge( $args, $tax_args );
104
+ }
105
+
106
+ // If post parent attribute, set up parent
107
+ if( $post_parent ) {
108
+ if( 'current' == $post_parent ) {
109
+ global $post;
110
+ $post_parent = $post->ID;
111
+ }
112
+ $args['post_parent'] = $post_parent;
113
+ }
114
+
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;
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
+ }
156
+ ?>
readme.txt ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Display Posts Shortcode ===
2
+ 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.2.1
7
+ Stable tag: 1.7
8
+
9
+ Display a listing of posts using the [display-posts] shortcode
10
+
11
+ == Description ==
12
+
13
+ The *Display Posts Shortcode* was written to allow users to easily display listings of posts without knowing PHP or editing template files.
14
+
15
+ Add the shortcode in a post or page, and use the arguments to query based on tag, category, post type, and many other possibilities (see the Arguments). I've also added some extra options to display something more than just the title: include_date, include_excerpt, and image_size.
16
+
17
+ See the [WordPress Codex](http://codex.wordpress.org/Class_Reference/WP_Query) for information on using the arguments.
18
+
19
+ The image_size can be set to thumbnail, medium, large (all controlled from Settings > Reading), or a [custom image size](http://codex.wordpress.org/Function_Reference/add_image_size).
20
+
21
+ = Examples =
22
+
23
+ [display-posts tag="advanced" posts_per_page="20"]
24
+ This will list the 20 most recent posts with the tag *Advanced*.
25
+
26
+ [display-posts tag="advanced" image_size="thumbnail"]
27
+ This will list the 10 most recent posts tagged *Advanced* and display a post image using the *Thumbnail* size.
28
+
29
+ [display-posts category="must-read" posts_per_page="-1" include_date="true" order="ASC" orderby="title"]
30
+ This will list every post in the Must Read category, in alphabetical order, with the date appended to the end.
31
+
32
+ [display-posts taxonomy="color" tax_term="blue" include_excerpt="true"]
33
+ This will display the title and excerpt of the 10 most recent posts marked "blue" in the custom taxonomy "color".
34
+
35
+ [display-posts wrapper="ol"]
36
+ This will display posts as an ordered list. Options are ul for unordered lists (default), ol for ordered lists, or div for divs.
37
+
38
+ [display-posts id="14,3"]
39
+ This will display only the posts with an ID of 14 and 3.
40
+
41
+ = Arguments =
42
+
43
+ * tag
44
+ * category
45
+ * posts_per_page
46
+ * id
47
+ * order
48
+ * orderby
49
+ * include_date
50
+ * include_excerpt
51
+ * image_size
52
+ * post_type
53
+ * post_parent
54
+ * taxonomy
55
+ * tax_term
56
+ * tax_operator
57
+ * wrapper
58
+
59
+ = Further Customizaion =
60
+
61
+ `display_posts_shortcode_args`
62
+ For customizing the $args passed to WP_Query. Useful if a query arg you want isn't already in the shortcode.
63
+ Example: http://www.billerickson.net/code/display-posts-shortcode-exclude-posts/
64
+
65
+ `display_posts_shortcode_output`
66
+ For customizing the output of individual posts.
67
+ Example: http://www.billerickson.net/code/display-posts-shortcode-full-content/
68
+
69
+ `display_posts_shortcode_wrapper_open`
70
+ `display_posts_shortcode_wrapper_close`
71
+ For customizing the outer markup of the whole listing. By default it is a `ul` but
72
+ can be changed to `ol` or `div` using the 'wrapper' attribute, or by using this filter.
73
+ Example: http://www.billerickson.net/code/display-posts-shortcode-outer-markup/
74
+
75
+ == Installation ==
76
+
77
+ 1. Upload `display-posts-shortcode` to the `/wp-content/plugins/` directory.
78
+ 1. Activate the plugin through the *Plugins* menu in WordPress.
79
+ 1. Add the shortcode to a post or page.
80
+
81
+
82
+ == Changelog ==
83
+
84
+ **Version 1.7**
85
+
86
+ * Added `id` argument to specify specific post IDs
87
+ * Added `display_posts_shortcode_args` filter in case the arguments you want aren't already included in the shortcode. See example: http://www.billerickson.net/code/display-posts-shortcode-exclude-posts/
88
+
89
+ **Version 1.6**
90
+
91
+ * Added `post_parent` where you can specify a parent by ID, or you can say `post_parent=current` and it will use the current page's ID.
92
+ * Added `wrapper` where you can decide if the posts are an unordered list, ordered list, or div's
93
+ * Added support for multiple taxonomy terms (comma separated) and taxonomy operator (IN, NOT IN, or AND).
94
+
95
+ **Version 1.5**
96
+ * For the sake of clarity I'm changing version numbers. No feature changes
97
+
98
+ **Version 0.1.5**
99
+ * Added a filter (display_posts_shortcode_output) so you can modify the output of individual posts however you like.
100
+
101
+ **Version 0.1.4**
102
+
103
+ * Added post_type, taxonomy, tax_term, and include_excerpt
104
+ * Added classes to each part of the listing (image, title, date, excerpt) to make it easier to change the look using CSS
105
+
106
+ **Version 0.1.3**
107
+
108
+ * Updated Readme
109
+
110
+ **Version 0.1.2**
111
+
112
+ * Added image_size option
113
+
114
+ **Version 0.1.1**
115
+
116
+ * Fix spacing issue in plugin
117
+
118
+ **Version 0.1**
119
+
120
+ * This is version 0.1. Everything's new!
121
+