Display Latest Posts on Homepage

This snippet displays a grid of the latest posts on your homepage, allowing visitors to quickly see your most recent content.

PHP

function display_latest_posts_on_homepage() {
    if (is_front_page()) {
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 6,
            'orderby' => 'date',
            'order' => 'DESC'
        );
        $latest_posts = new WP_Query($args);
        if ($latest_posts->have_posts()) {
            echo '<div class="latest-posts-grid">';
            while ($latest_posts->have_posts()) {
                $latest_posts->the_post();
                echo '<div class="post-item">';
                echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
                echo '<p>' . get_the_excerpt() . '</p>';
                echo '</div>';
            }
            echo '</div>';
            wp_reset_postdata();
        }
    }
}
add_action('wp_footer', 'display_latest_posts_on_homepage');

Snippet Feedback

Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.

Language

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.