Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display recent posts in the front-page.php (Home) in Wordpress?

Tags:

wordpress

I know that traditionally you can have 2 main pages: the static page (front-page.php)and the page for the last posts(home.php).

Right now, front-page.php (Home) is my "index" page. It has some content (like a tagline), but now I want my last post to be displayed bellow that content.

Like this (front-page.php):

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

get_header(); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    <?php the_content(); ?>  <--this is the tagline of my main page
    <div class="line2"></div>

<?php endwhile; ?>

 <<<<<<MY LAST POST HERE>>>>>>

    </div><!-- #content -->

    <?php get_sidebar(); ?>
    <?php get_footer(); ?> 
like image 680
alexchenco Avatar asked Nov 16 '25 03:11

alexchenco


1 Answers

Use get_posts() and do a basic loop to output the title, content or whatever you like, it will work just like a regular loop, for example..

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

get_header(); ?>

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>

    <?php the_content(); ?>  <--this is the tagline of my main page
    <div class="line2"></div>

    <?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

<!-- Start latest post -->

<?php $latest_post = get_posts( 'numberposts=1' ); // Defaults args fetch posts starting with the most recent ?>
<?php foreach( $latest_post as $post ) : setup_postdata( $post ); ?>

    <?php the_title(); ?><br />
    <?php the_content(); ?>

<?php endforeach; ?>
<?php wp_reset_query(); ?>

<!-- End latest post -->

    </div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?> 

Reference for functions used in the above.
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/wp_reset_query

Hope that helps.. :)

like image 172
t31os Avatar answered Nov 17 '25 20:11

t31os



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!