我无法弄清楚如何确保只有我最近的帖子显示在我的front-page.php文件的首页上,因为我有一些自定义的帖子类型,并希望我的最新帖子到只显示在我最近的自定义帖子类型下面。
很抱歉,如果这没有意义,基本上我只是想知道要添加到wordpress循环中的内容,以确保它只给我最新的帖子,而不是其他人。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="section_title">
<h2>Recent Update:</h2>
</div>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<p><a href="<?php the_permalink(); ?>" class="btn right">Read More</a></p>
</div>
<div class="front_page_image">
<img src="images/annoyed_victoria.jpg">
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
答案 0 :(得分:0)
您可以使用pre_get_posts挂钩将posts_per_page限制为1。
将以下内容添加到functions.php。
function my_main_query($query){
if( !is_admin() && $query->is_main_query() ){
$query->set( 'posts_per_page', 1 );
}
}
add_action('pre_get_posts','my_main_query');
感谢Nathan Dawson指出query_posts()的缺陷。
有关循环以及如何更改循环的详细信息,请参阅Wordpress Codex。
答案 1 :(得分:0)
试试这个,它应该获取最近的5个帖子。
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
<a href="<?php the_permalink() ?>"><span class="visit">See post</span></a>
<?php endwhile;?>