skip to Main Content

How to Create a Recent Posts Widget in WordPress with Code (Step-by-Step Guide)

How to Create a Recent Posts Widget in WordPress with Code (Step-by-Step Guide)

Table of Contents

  1. Introduction
  2. Why Create a Custom Recent Posts Widget?
  3. Step 1: Register Your Widget
  4. Step 2: Display Recent Posts in the Widget
  5. Step 3: Style the Widget (Optional)
  6. Conclusion
  7. FAQs

Introduction

Do you want to make your WordPress site show recent blog posts without using a plugin? In this tutorial, you will learn how to create a recent posts widget in WordPress with code. It’s simple and perfect for improving site navigation.

If you’re new to WordPress widgets or want to explore more about how widgets work, check out our related guide:
👉 How to Set Up and Use WordPress Widgets for Better Site Functionality


Why Create a Custom Recent Posts Widget?

Using your own code has several benefits:

  • No extra plugins (faster site).
  • Full control over layout.
  • Custom features (like thumbnails or dates).

Step 1: Register Your Widget

Add the following code to your theme’s functions.php file or in a custom plugin:

function custom_recent_posts_widget() {
    register_widget( 'Custom_Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'custom_recent_posts_widget' );

class Custom_Recent_Posts_Widget extends WP_Widget {
    function __construct() {
        parent::__construct(
            'custom_recent_posts_widget',
            __('Custom Recent Posts', 'text_domain'),
            array( 'description' => __( 'Displays recent blog posts.', 'text_domain' ) )
        );
    }

    public function widget( $args, $instance ) {
        echo $args['before_widget'];
        if ( ! empty( $instance['title'] ) ) {
            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
        }

        $recent_posts = new WP_Query( array(
            'posts_per_page' => 5,
            'post_status'    => 'publish',
        ) );

        if ( $recent_posts->have_posts() ) {
            echo '<ul>';
            while ( $recent_posts->have_posts() ) {
                $recent_posts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        }

        echo $args['after_widget'];
    }

    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Recent Posts', 'text_domain' );
        ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
                <?php esc_attr_e( 'Title:', 'text_domain' ); ?>
            </label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
                   name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
                   value="<?php echo esc_attr( $title ); ?>">
        </p>
        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = sanitize_text_field( $new_instance['title'] );
        return $instance;
    }
}


Step 2: Display Recent Posts in the Widget

After saving the code:

  1. Go to Appearance > Widgets.
  2. Drag the “Custom Recent Posts” widget to your sidebar or footer.
  3. Add a title if you want.

Step 3: Style the Widget (Optional)

You can add some CSS to style the list. For example, in your style.css:

.custom_recent_posts_widget ul {
  padding-left: 20px;
}

.custom_recent_posts_widget ul li {
  margin-bottom: 10px;
}

You can also add thumbnails or post dates if needed.


Conclusion

You now know how to create a recent posts widget in WordPress with code. This gives you full control without needing plugins. It’s a great way to show the latest content and improve your website’s user experience.


FAQs

Can I use this code in any theme?

Yes, you can use this code in any WordPress theme. Just add it to your functions.php or a custom plugin.

Can I show thumbnails or post dates?

Yes! You can edit the loop part to include get_the_post_thumbnail() and get_the_date() functions.

How many posts will it show?

By default, it shows 5 posts. You can change 'posts_per_page' => 5 to any number.

Will it break my website if something is wrong?

Always back up your site before editing code. You can also use a plugin like Code Snippets to test safely.

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