如何在WordPress的front-page.php(Home)中显示最近的帖子?

ale*_*nco 3 wordpress

我知道传统上你可以有2个主页:静态页面(front-page.php)和最后一个帖子的页面(home.php).

现在,front-page.php(Home)是我的"索引"页面.它有一些内容(如标语),但现在我希望我的上一篇文章显示在内容之下.

像这样(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(); ?> 
Run Code Online (Sandbox Code Playgroud)

t31*_*1os 9

使用get_posts()并执行基本循环输出标题,内容或任何您喜欢的内容,它将像常规循环一样工作,例如..

<?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(); ?> 
Run Code Online (Sandbox Code Playgroud)

参考上面使用的功能.
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/wp_reset_query

希望有帮助.. :)