How to Display Posts Category Using a Shortcode in WordPress

⏲️ Estimated reading time: 3 min

Display posts from a specific category inside a WordPress page or post can enhance user experience by organizing related content. Instead of manually updating post lists, you can use a WordPress shortcode to dynamically display category-based posts.


In this tutorial, we will create a custom shortcode that allows you to insert category posts anywhere on your website.


Why Use a Shortcode for Category Posts?

Using a shortcode for displaying category posts provides multiple advantages:

1️⃣ Dynamic Content: Shortcode automatically updates when new posts are added to a category.
2️⃣ Easy to Use: No coding required after setup just insert the shortcode in any post or page.
3️⃣ Customization: You can modify the output to include excerpts, images, or custom styles.
4️⃣ Performance Friendly: Uses WP_Query to fetch only necessary data.


Display Posts Category Using a Shortcode in WordPress

1: Add the Display Posts Shortcode Function

To enable this feature, add the following PHP function to your functions.php file or in a custom plugin:

function get_category_posts_shortcode($atts) {
    $atts = shortcode_atts(array(
        'category' => '',
        'limit'    => 5
    ), $atts, 'category_posts');

    if (empty($atts['category'])) {
        return 'Please specify a category.';
    }

    $args = array(
        'category_name'  => sanitize_text_field($atts['category']),
        'posts_per_page' => intval($atts['limit']),
        'post_status'    => 'publish'
    );

    $query = new WP_Query($args);

    if (!$query->have_posts()) {
        return 'No posts found in this category.';
    }

    ob_start();
    echo '<ul class="category-posts-list">';
    while ($query->have_posts()) {
        $query->the_post();
        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    }
    echo '</ul>';
    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode('category_posts', 'get_category_posts_shortcode');

2: How to Use the Display Posts Shortcode

After adding the function, you can display category-specific posts anywhere on your site using this shortcode:

[category_posts category="technology" limit="5"]

🔹 Replace "technology" with the slug of your target category.
🔹 Adjust "limit" to change the number of displayed posts.

For example:

Displays 3 posts from the News category.

[category_posts category="news" limit="3"]

Displays 10 posts from the Recipes category.

[category_posts category="recipes" limit="10"]

3: Display Posts With Excerpts and Styling

To make the list more engaging, you can modify the function to include post excerpts:

echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>';
echo '<p>' . wp_trim_words(get_the_excerpt(), 20, '...') . '</p></li>';

This will show a short preview (20 words) below each post title.

To add some CSS styling, include this in your theme’s style.css file:

.category-posts-list {
    list-style: none;
    padding: 0;
}

.category-posts-list li {
    margin-bottom: 15px;
}

.category-posts-list a {
    font-size: 18px;
    font-weight: bold;
    color: #0073aa;
    text-decoration: none;
}

.category-posts-list p {
    font-size: 14px;
    color: #555;
}

4: Benefits of Using Display Posts Shortcode

🔥 Better User Engagement

Displaying related posts keeps visitors on your site longer, reducing bounce rates.

🎯 SEO Benefits

Internal linking boosts SEO by improving site structure and indexability for search engines.

⚙️ Flexible Customization

You can add featured images, post dates, and more to make the output visually appealing.


Conclusion

This shortcode is a powerful way to display category posts dynamically. By adding excerpts, styles, and other elements, you can customize it to fit your website’s design.

🚀 Try it out and let me know if you need any modifications!


Tags:

WordPress, Shortcode, Category Posts, WP_Query, WordPress Development, PHP, Custom Shortcode, Blog Categories, WordPress Coding, How-To Guide


Report an issue (max 5 words):

Only logged-in users can submit reports.


Discover more from HelpZone

Subscribe to get the latest posts sent to your email.

Want to support us? Let friends in on the secret and share your favorite post!

Photo of author

Flo

How to Display Posts Category Using a Shortcode in WordPress

Published

Update

Welcome to HelpZone.blog, your go-to hub for expert insights, practical tips, and in-depth guides across technology, lifestyle, business, entertainment, and more! Our team of passionate writers and industry experts is dedicated to bringing you the latest trends, how-to tutorials, and valuable advice to enhance your daily life. Whether you're exploring WordPress tricks, gaming insights, travel hacks, or investment strategies, HelpZone is here to empower you with knowledge. Stay informed, stay inspired because learning never stops! 🚀

👍 Like us on Facebook!

Closing in 10 seconds

Leave a Reply