[su_posts template="templates/teaser-loop.php" post_type="docs" taxonomy="docs_category" tax_term="shortcodes" posts_per_page="4" orderby="rand"]
Table of contents
The [su_posts]
shortcode is intended for display of posts, pages, and various post types. You can display posts from a specific category or by a specific tag. You can also choose multiple taxonomies and select the number of displayed posts. This shortcode uses WP_Query class.
Option name | Possible values | Default value |
---|---|---|
template Relative path to the template file. Default templates placed in the plugin directory (templates folder). You can copy them under your theme directory and modify as you want. You can use following default templates that already available in the plugin directory: templates/default-loop.php – posts loop templates/teaser-loop.php – posts loop with thumbnail and title templates/single-post.php – single post template templates/list-loop.php – unordered list with posts titles | Any text value | templates/default-loop.php |
id Enter comma separated ID’s of the posts that you want to show | Any text value | – none – |
posts_per_page Specify number of posts that you want to show. Enter -1 to get all posts | Number from -1 to 10000 | 12 |
post_type Select post types. Hold Ctrl key to select multiple post types | Post type slug(s) separated by comma(s) | post |
taxonomy Select taxonomy to show posts from | Taxonomy slug(s) separated by comma(s) | category |
tax_term Select terms to show posts from | Term slug(s) separated by comma(s) | – none – |
tax_operator Operator to test | IN (IN – posts that have any of selected categories terms) NOT IN (NOT IN – posts that is does not have any of selected terms) AND (AND – posts that have all selected terms) | IN |
author Enter here comma-separated list of author’s IDs. Example: 1,7,18 | Any text value | – none – |
meta_key Enter meta key name to show posts that have this key | Any text value | – none – |
offset Specify offset to start posts loop not from first post | Number from 0 to 10000 | 0 |
order Posts order | desc (Descending) asc (Ascending) | DESC |
orderby Order posts by | none (None) id (Post ID) author (Post author) title (Post title) name (Post slug) date (Date) modified (Last modified date) parent (Post parent) rand (Random) comment_count (Comments number) menu_order (Menu order) meta_value (Meta key values) meta_value_num (Meta key values (Numeric)) | date |
post_parent Show childrens of entered post (enter post ID) | Any text value | – none – |
post_status Show only posts with selected status | publish (Published) pending (Pending) draft (Draft) auto-draft (Auto-draft) future (Future post) private (Private post) inherit (Inherit) trash (Trashed) any (Any) | publish |
ignore_sticky_posts Set to “yes” to prevent sticky posts from being moved to the start of the returned list of posts. They are still included, but appear in regular order | yes or no | no |
Unfortunately, pagination is currently not available in the shortcode. This function will be added in future versions.
[su_posts]
shortcode includes various templates which you can use to display your posts. Built-in templates are located in the following folder:
/wp-content/plugins/shortcodes-ultimate/includes/partials/shortcodes/posts/templates/
The default template that includes post featured image, post date, title, excerpt, and comments link.
[su_posts template="templates/default-loop.php"]
A simplified version of the default template which only includes post featured image and title.
[su_posts template="templates/teaser-loop.php"]
A template which only shows a single post at a time. Includes post title, post date, comments link, and the post content.
[su_posts template="templates/single-post.php"]
A template that shows queried posts as a plain HTML list.
[su_posts template="templates/list-loop.php"]
Do not edit templates in the plugin folder, since all your changes will be lost after plugin update.
To modify a built-in template you should copy it to your theme directory first. For convenience, you can copy the whole templates
folder from the plugin folder. You can rename the copied folder, it doesn’t have to be templates
. Resulting path to template files should look like so:
/wp-content/themes/THEME-NAME/templates/
Now you can edit copied template files.
Use the template
attribute to change the template used by shortcode. The value of this attribute must be a template file path relative to your theme’s folder. Example:
[su_posts template="templates/default-loop.php"]
In the example above the plugin will search for a template in the following locations (in the specified order):
/wp-content/themes/child-theme/templates/default-loop.php
/wp-content/themes/parent-theme/templates/default-loop.php
/wp-content/plugins/shortcodes-ultimate/includes/partials/shortcodes/posts/templates/default-loop.php
The [su_posts]
shortcode supports custom templates, which makes it incredibly powerful. Follow the steps below to create one.
Step 1
Create a new folder in your theme directory and name it su-posts-templates
, so the resulting path would be:
/wp-content/themes/YOUR-THEME-NAME/su-posts-templates/
Step 2
Create a new file in the su-posts-templates
folder and name it my-template.php
. The resulting path would be:
/wp-content/themes/YOUR-THEME-NAME/su-posts-templates/my-template.php
Step 3
Put the following snippet into the created file:
<?php if ( $posts->have_posts() ) : ?>
<div class="su-posts su-posts-my-template">
<?php while ( $posts->have_posts() ) : ?>
<?php $posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
</div>
<?php else : ?>
<p>Posts not found!</p>
<?php endif; ?>
Step 4
Now you can use the created template:
[su_posts template="su-posts-templates/my-template.php"]
If you need to store your templates outside of the theme directory, you can extend the list of allowed template paths. Use the following snippet in a custom plugin or in the functions.php
file:
add_filter(
'su/shortcode/posts/allowed_template_locations',
function( $locations ) {
// /wp-content/
$locations[] = WP_CONTENT_DIR;
return $locations;
},
10,
1
);
The code above will allow you to store templates under /wp-content/
directory.