skip to Main Content

How to Create a Dynamic WordPress Post List with Shortcodes

How to Create a Dynamic WordPress Post List with Shortcodes – Tutorial Guide

Table of Contents


    Introduction

    Displaying a list of posts dynamically in WordPress can improve content visibility and enhance user experience. The easiest way to do this is by using shortcodes. In this guide, you will learn how to create and use shortcodes to generate dynamic post lists on your website.


    What Are WordPress Shortcodes?

    Shortcodes are small snippets of code that let you insert dynamic content into WordPress posts, pages, and widgets. They are enclosed in square brackets, like [your_shortcode], and can execute custom PHP functions.


    Why Use Shortcodes for Post Lists?

    • They allow easy customization without modifying theme files.
    • You can reuse them anywhere on your website.
    • They enable dynamic content updates based on criteria such as category, tag, or date.

    Creating a Basic WordPress Post List Shortcode

    You can create a simple shortcode to display a list of posts by adding the following code to your theme’s functions.php file:

    function custom_post_list_shortcode($atts) {
        $atts = shortcode_atts(
            array(
                'category' => '',
                'posts_per_page' => 5,
            ),
            $atts,
            'post_list'
        );
        
        $args = array(
            'posts_per_page' => $atts['posts_per_page'],
            'category_name' => $atts['category'],
        );
        
        $query = new WP_Query($args);
        $output = '<ul>';
        
        while ($query->have_posts()) {
            $query->the_post();
            $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        
        wp_reset_postdata();
        $output .= '</ul>';
        return $output;
    }
    add_shortcode('techykp_post_list', 'custom_post_list_shortcode');
    

    Now, you can use [techykp_post_listposts_per_page="5" category="news"] in your WordPress editor to display posts dynamically.


    Customizing the Post List Output

    You can modify the shortcode to:

    • Display post thumbnails.
    • Show excerpts.
    • Filter posts by tag, author, or date.

    For example, adding the following line inside the while loop will show post excerpts:

    $output .= '<p>' . get_the_excerpt() . '</p>';
    

    Adding Shortcodes to Pages and Widgets

    To add your shortcode to a page, simply place it in the WordPress editor:

    [techykp_post_list posts_per_page="5" category="blog"]
    

    You can also use it in widgets by adding a Shortcode Block or Text Widget in your sidebar.


    Using Plugins for Advanced Post Lists

    If you prefer not to edit theme files, plugins like Display Posts or WP Show Posts offer user-friendly interfaces for listing posts dynamically.


    FAQs

    Can I display posts from multiple categories?

    Yes, modify the shortcode like this: [techykp_post_list category="news,events"].

    How do I add featured images to the post list?

    Use get_the_post_thumbnail() inside the while loop:
    $output .= get_the_post_thumbnail(get_the_ID(), ‘thumbnail’);

    Does this method work with custom post types?

    Yes! Modify the $args array to include 'post_type' => 'your_custom_post_type'.

    I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

    This Post Has 0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Back To Top