How do I display my most recent post on my home page? (coding)

I’m building a theme, and i want to display the most recent blog post on the home page as an excerpt (which is how i display them on the blog page) I only want to display one, and i want it to update to always show the most recent one. How would i do this?

You do this from the query you use in your loop. This reference for WP_Query(), https://codex.wordpress.org/Class_Reference/WP_Query, lists all the possible parameters you can use to customize your loop. So for this one, all you need is:

$args = array(
  'posts_per_page' => 1
);

$homepage_query = new WP_Query( $args );

if ( $homepage_query->have_posts() ) : while ( $homepage_query->have_posts() ) : $homepage_query->the_post();

       the_excerpt();

endwhile; endif;
				
wp_reset_postdata(); 

This will always only show one excerpt, and because posts are normally ordered from most recent to oldest, the one that will show will be the most recent.

And where does this need to go please?

Do you have a separate template for your home page, perhaps named front-page.php?

yes

here is the code:

<?php
/*
	Template Name: Home Page
*/

$intro_title		= get_field('home_intro_title');
$intro_body			= get_field('home_intro_body');

get_header(); ?>

	<div class="container">
      <div class="row">
        <article class="col-sm-12">
        <div class="intro">
          
           <?php if( !empty(intro_title) ) : ?>
           
            <h2><?php echo $intro_title ?></h2>
            
           <?php endif ?>
           
            <p><?php echo $intro_body; ?></p>
            
          </div>
        </article>
      </div>
      
      <div class="row">
        <div class="col-sm-8">
         
         
          
        </div>
        
        <div class="col-sm-4">
			<?php get_sidebar(); ?>
		</div>

        <?php get_templat_part( 'content', 'bio' ); ?>
        
		</div>

<?php get_footer(); ?>

Watch the typo.

Where did you want to put the excerpt - inside the <div class="col-sm-8">?

thanks for pointing that out, and yes.

Then just copy and paste that code that I gave you inside that <div> and see what happens. Don’t forget to enclose it in <?php ... ?> tags.

so it kind of worked, it displayed the excerpt, but not the title or any of the styling

this is the code in content.php:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?><?php edit_post_link( 'edit &raquo;', '', '' ); ?>
		
		<hr>
		
		<?php if ( 'post' === get_post_type() ) : ?>		
		<?php endif; ?>

	</header><!-- .entry-header -->

		<?php if ( has_post_thumbnail() ) { ?>
		<div class="post-image">
			<?php the_post_thumbnail(); ?>
		</div>
        <?php } ?>
            
<?php if ( is_single() ) : ?>
<div class="post-body">
    <?php the_content(); ?>
</div><!-- post-content -->
<?php else : ?>
<div class="post-excerpt">
    <?php the_excerpt(); ?>
</div><!-- post-excerpt -->
<?php endif; ?>
	
</article><!-- #post-<?php the_ID(); ?> -->

You didn’t say you wanted the title also. Here it is:

<?php
$args = array(
  'posts_per_page' => 1
);

$homepage_query = new WP_Query( $args );

if ( $homepage_query->have_posts() ) : while ( $homepage_query->have_posts() ) : $homepage_query->the_post();
?>

<h1><?php the_title(); ?></h1>

<?php

    the_excerpt();

endwhile; endif;
				
wp_reset_postdata();

?>

I used <h1> tags for the title, but feel free to change those to whatever makes most sense semantically.

And what styling are you referring too?

Have you read the WordPress documentation on creating themes? If not, you will find a lot of very useful information there.

okay, thank you very much! The styles are in the normal style.css file

If you are using Bootstrap, you have to make sure that your html contains the class names to go along with the styles in the style.css file.

yeah, i just realized that, and its now working perfectly! Thank you so so much for all your help!

1 Like

If you are using WORDPRESS CMS, you will get all the recent article on your homepage till you are not using Static Homepage.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.