wordpress - 如何在Wordpress中的front-page.php(主页)中显示最近的帖子?

标签 wordpress

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

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

像这样(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(); ?> 

最佳答案

使用 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(); ?> 

上面使用的函数引用。
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/wp_reset_query

希望有帮助..:)

关于wordpress - 如何在Wordpress中的front-page.php(主页)中显示最近的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4507732/

相关文章:

wordpress - 联系表格 7 显示已发送消息但未收到电子邮件

jquery - 从 Soundcloud 上的 iframe 中删除元素

php - Woocommerce 手动添加订单 : How to filter products in ajax call

javascript - Wordpress wp_dropdown_pages 未通过 jQuery 附加

javascript - 当用户使用浏览器的后退按钮时如何删除 css 类

php - 使用 dbDelta 函数在 Wordpress 中创建表

php - LIKE 子句在 wordpress 的 $wpdb->prepare() 中不起作用

javascript - 在 WordPress 站点中加载 jquery.js 后加载 javascript 代码

wordpress - 如何获得术语元 WPGraphQL/WooGraphQL?

php - 使用 WPML 插件更改所有语言的 "return to shop"URL