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.
SHARED BY