Home
Clone this wiki locally
The Display Posts Shortcode was written to allow users to easily display listings of posts without knowing PHP or editing template files.
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.
See the WordPress Codex for information on using the arguments.
WordPress.com Users
This plugin is now available to all users of WordPress.com. There’s no installation necessary, just drop the shortcode in your page’s content area. Unfortunately you won’t be to use any of the filters listed in Further Customization since this requires customizing your theme, which isn’t allowed on WordPress.com
Image Alignment
A common request is display a list of posts with title, excerpt, and the thumbnail aligned to the left. Here’s the shortcode you might use:
This includes the excerpt, adds an image of the “thumbnail” size (you can customize the image sizes in Settings > Media), and tells it to wrap the list in a div instead of an unordered list so we don’t have bullets.
This plugin does not include any styling of the listings so that you can make them look however you want. In order to get the image floating to the left, add this to your theme’s style.css:
.display-posts-listing .listing-item {
clear: both;
}
.display-posts-listing img {
float: left;
margin: 0 10px 10px 0;
}
Examples
This will list the 20 most recent posts with the tag ‘Advanced’.
This will list the 10 most recent posts tagged ‘Advanced’ and display a post image using the ‘Thumbnail’ size.
This will list every post in the Must Read category, in alphabetical order, with the date appended to the end.
This will display the title and excerpt of the 10 most recent posts marked “blue” in the custom taxonomy “color”.
This will display posts as an ordered list. Options are ul for unordered lists (default), ol for ordered lists, or div for divs.
This will display only the posts with an ID of 14 and 3.
Arguments
author
Specify the post author
Default: empty
Example:
category
Specify the category slug (or comma separated list of category slugs)
Default: empty
Example:
date_format
Specify the date format used when include_date is true. See Formatting Date and Time on the Codex for more information.
Default: ‘(n/j/Y)’
Example:
- Candidate-1 Job-1 June 9, 2016
- New Test Post March 24, 2015
- Hello world! October 28, 2014
- Sample Post With an Ordered List July 1, 2011
id
Specify a specific post ID (or multiple post IDs) to display.
Default: empty
Example:
image_size
Specify an image size for displaying the featured image, if the post has one. The image_size can be set to thumbnail, medium, large (all controlled from Settings > Reading), or a custom image size.
Default: empty
Example:
include_date
Include the post’s date after the post title. The default format is (7/30/12), but this can be customized using the ‘date_format’ parameter.
Default: empty
Example
- Candidate-1 Job-1 (6/9/2016)
- New Test Post (3/24/2015)
- Hello world! (10/28/2014)
- Sample Post With an Ordered List (7/1/2011)
include_excerpt
Include the post’s excerpt after the title (and date if provided).
Default: empty
Example:
- Candidate-1 Job-1 - General: Activity Lifecycle, Fragments, Broadcast Receivers, Services, XML layout, custom widgets, streaming video, camera API, Account Manager,geolocation, motion sensor, Bluetooth Environment: Android SDK, Android Studio, Gradle, Eclipse Libraries: Robolectric, Picasso, Otto Bus, EventBus, Dagger Reporting: Fabric, Google analytics, Localytics, Appsee Java: objectoriented design, multithreading, unit testing, debugging RESTful API: HttpGet / HttpPost, XML / JSON… Read More
- New Test Post - Id diam volutpat condimentum non a eros lorem ad hendrerit curabitur fringilla scelerisque dui volutpat a dictumst ad lacinia molestie hac dignissim euismod torquent parturient parturient consectetur. Hendrerit conubia orci convallis pulvinar imperdiet mattis penatibus a a venenatis porta suspendisse ut congue vestibulum montes velit habitasse facilisi ultrices cubilia a habitasse taciti dis. Ante commodo… Read More
- Hello world! - Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
- Sample Post With an Ordered List - This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what exactly is on your mind. Ordered list item #1 Ordered… Read More
offset
The number of posts to pass over
Default: 0
Example:
order
Specify whether posts are ordered in descending order (DESC) or ascending order (ASC).
Default: DESC
Example:
orderby
Specify what the posts are ordered by. See the available parameters here.
Default: date
Example:
post_parent
Display the pages that are a child of a certain page. You can either specify an ID or ‘current’, which displays the children of the current page.
Default: empty
Example:
post_status
Show posts associated with a certain post status
Default: publish
Example:
post_type
Specify which post type to use. You can use a default one (post or page), or a custom post type you’ve created.
Default: post
Example:
posts_per_page
How many posts to display.
Default: 10
Example:
tag
Display posts from a specific tag, or tags. You must use the tag slug(ex: example-tag), not the tag’s name (ex: Example Tag).
Default: empty
Example:
taxonomy, tax_term, and tax_operator
Use these parameters to do advanced taxonomy queries. Use ‘taxonomy’ for the taxonomy you’d like to query, ‘tax_term’ for the term slug (or terms) you’d like to include, and ‘operator’ to change how the query uses those terms (most likely this field will not be needed).
Default: ‘taxonomy’ = empty , ‘tax_term’ = empty , ‘tax_operator’ = ‘IN’
Example:
title
Give the div a title heading
Default: empty
Example:
Recent Posts
Result:<h2 class="display-posts-title">Recent Posts</h2>
inside the wrapper div. The heading tag is filterable via display_posts_shortcode_title_tag
wrapper
What type of HTML should be used to display the listings. It can be an unordered list (ul), ordered list (ol), or divs (div) which you can then style yourself.
Default: ul
Example:
Multiple Taxonomy Queries
While most people will only ever need a single taxonomy query, this plugin supports an infinite number of taxonomy queries. Let’s say you wanted to get all posts in category “featured” and also tagged “homepage”. We’ll use a shortcode that looks like this:
You can string as many of those as you like, just start the count at 2. In the field listing below, replace (count) with an actual number.
Here’s the available fields:
taxonomy_(count)
Which taxonomy to query
Default: empty
tax_(count)_term
Which terms to include (if more than one, separate with commas)
Default: empty
tax_(count)_operator
How to query the terms (IN, NOT IN, or AND)
Default: IN
tax_relation
Describe the relationship between the multiple taxonomy queries (should the results match all the queries or just one of them). Available options: AND and OR
Default: AND
Further Customization
shortcode_atts_display-posts
Change the default arguments of the shortcode. For instance, if you want all shortcodes to have include_excerpt=”true” and include_author=”true”, you could use this to set it once and then not have to include those parameters on all shortcodes.
display_posts_shortcode_args
For customizing the $args passed to WP_Query. Useful if a query arg you want isn’t already in the shortcode. Example: http://www.billerickson.net/code/display-posts-shortcode-exclude-posts/
display_posts_shortcode_output
For customizing the output of individual posts. Example: http://www.billerickson.net/code/display-posts-shortcode-full-content/
display_posts_shortcode_no_results
Content to display if no posts are found (default is empty).
display_posts_shortcode_wrapper_open and display_posts_shortcode_wrapper_close
For customizing the outer markup of the whole listing. By default it is a ul
but can be changed to ol
or div
using the ‘wrapper’ attribute, or by using this filter. Example: http://www.billerickson.net/code/display-posts-shortcode-outer-markup/
display_posts_shortcode_post_class
For adding classes to the individual posts (ex: break into columns)
Adding a target=”_blank” to the anchor tag in posts
Add the following code to your theme’s functions.php file: function be_links_in_new_window( $output ) { return str_replace( ‘<a ‘, ‘<a target=”_blank” ‘, $output ); } add_filter( ‘display_posts_shortcode_output’, ‘be_links_in_new_window’, 20 );
More examples: http://www.billerickson.net/code-tag/display-posts-shortcode/