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

I know that traditionally you can have two main pages: a static page ( front-page.php) and a page for recent posts ( home.php).

Right now, front-page.php (Home) is my "index" page. It has some content (e.g. tagline), but now I want my last message to be displayed below 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(); ?> 
+3
source share
1 answer

Use get_posts()and create a basic loop to display the title, content, or whatever you like, it will work 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(); ?> 

, .
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/wp_reset_query

, ..:)

+9

Source: https://habr.com/ru/post/1781620/


All Articles