Shortcode: List Posts by Tag with Excerpt in WordPress
If you want to display a list of posts filtered by a specific tag along with their excerpts anywhere on your WordPress site, using a shortcode is a practical and flexible solution. This tutorial shows you how to create a custom shortcode that lists posts by tag with excerpts, so you can easily embed it in posts, pages, or widgets.
When to Use This Shortcode
- You want to highlight related content by tag on a page or post.
- You need a dynamic list of posts filtered by tags without manually updating links.
- You want to show post excerpts alongside titles for better context.
- You prefer a shortcode solution that can be reused and customized easily.
Quick Fix: Create a Shortcode to List Posts by Tag with Excerpt
- Add the provided PHP code to your theme’s
functions.php
file or create a mini-plugin. - Use the shortcode
[posts_by_tag tag="your-tag-slug" posts="5"]
in your content. - Adjust parameters like tag slug and number of posts as needed.
- Test the shortcode on a page or post to verify output.
Why This Happens
WordPress does not have a built-in shortcode to list posts filtered by tag with excerpts. The default shortcodes like [recent_posts]
or widgets do not provide flexible tag filtering combined with excerpts. Creating a custom shortcode leverages WordPress’s WP_Query
class to fetch posts by tag and outputs them in a clean, reusable format.
Step-by-Step: Add the Shortcode Code
Below is the complete PHP code for the shortcode. It accepts two attributes:
tag
: The slug of the tag to filter posts by (required).posts
: Number of posts to display (optional, default is 5).
<?php
// Shortcode to list posts by tag with excerpt
function shortcode_list_posts_by_tag_with_excerpt($atts) {
// Set default attributes and merge with user input
$atts = shortcode_atts(
array(
'tag' => '', // Tag slug to filter by
'posts' => 5 // Number of posts to show
),
$atts,
'posts_by_tag'
);
// Sanitize inputs
$tag_slug = sanitize_text_field($atts['tag']);
$posts_per_page = intval($atts['posts']);
if ($posts_per_page <= 0) {
$posts_per_page = 5;
}
if (empty($tag_slug)) {
return '<p>Please provide a tag slug in the shortcode attribute.</p>';
}
// Query posts by tag
$query_args = array(
'tag' => $tag_slug,
'posts_per_page' => $posts_per_page,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$query = new WP_Query($query_args);
if (!$query->have_posts()) {
return '<p>No posts found for tag: ' . esc_html($tag_slug) . '</p>';
}
// Start output buffering
ob_start();
echo '<ul class="posts-by-tag-list">';
while ($query->have_posts()) {
$query->the_post();
echo '<li>';
echo '<a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
echo '<p class="post-excerpt">' . esc_html(get_the_excerpt()) . '</p>';
echo '</li>';
}
echo '</ul>';
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('posts_by_tag', 'shortcode_list_posts_by_tag_with_excerpt');
?>
How to Add This Code
- Via
functions.php
: Open your active theme’sfunctions.php
file and paste the above code at the end. - Via Mini-Plugin: Create a new file named
posts-by-tag-shortcode.php
inwp-content/plugins/
with the following header and the code above:
<?php
/*
Plugin Name: Posts By Tag Shortcode
Description: Adds a shortcode to list posts by tag with excerpts.
Version: 1.0
Author: Your Name
*/
// Paste the shortcode function code here
?>
Then activate the plugin from the WordPress admin.
Testing the Shortcode
- Create or edit a post/page where you want to display the list.
- Insert the shortcode with your desired tag slug and number of posts, for example:
[posts_by_tag tag="news" posts="3"]
This will display the 3 most recent posts tagged with news
, showing their titles linked to the posts and excerpts below.
Variations and Customizations
- Change number of posts: Adjust the
posts
attribute, e.g.posts="10"
. - Style output: Add CSS targeting
.posts-by-tag-list
and.post-excerpt
classes. - Show full content: Replace
get_the_excerpt()
withget_the_content()
in the code. - Order posts: Modify
$query_args
to include'orderby' => 'date'
or other parameters. - Multiple tags: Extend the shortcode to accept comma-separated tags by adjusting the query.
Works On
Environment | Notes |
---|---|
Apache, Nginx, LiteSpeed | Compatible with all standard web servers running WordPress. |
cPanel, Plesk | Works on hosting control panels with standard WordPress installations. |
PHP
Developer Snippets
Tags:Loop, Shortcode, Tag
|