所以我试图只从WordPress中拉出最新的粘性帖子,只有1个。我使用'showpost=1',但出于某种原因,如果我有两个帖子标记为粘都显示?
<h2>Breaking News</h2>
<?php
query_posts('posts_per_page=1');
if (have_posts()) {
while (have_posts()) : the_post();
if ( is_sticky() ) : ?>
<div class="small-12 medium-6 large-4 columns">
<div class="img-holder">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('sticky-hp-thumbnails');
}
?>
<?php if( get_field('sticky_sub_heading') ): ?>
<div class="tag">
<p>
<?php the_field('sticky_sub_heading'); ?>
</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="small-12 medium-6 large-4 columns">
<h3><a href"<?php the_permalink() ?>">
<?php the_title(); ?>
</a></h3>
<?php if( get_field('sticky_date') ): ?>
<p class="sticky-date">
<?php the_field('sticky_date'); ?>
</p>
<?php endif; ?>
<p>
<?php the_field('sticky_summary'); ?>
</p>
<a href="<?php the_permalink() ?>" class="button">Read More</a> </div>
<?php endif;
endwhile;
}
wp_reset_query();
?>
我在上面的代码哪里出错了?
用posts_per_page代替"showposts"
。
query_posts( 'posts_per_page=1' );
来源:https://codex.wordpress.org/Function_Reference/query_posts
更新:添加WP_QUERY代码获取最新的黏贴:
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post">
<h3><?php echo get_the_title(); ?></h3></a>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
解决问题的最简单方法是删除while循环。这样你将只打印一个。
当然,这是次优的,因为其他页面可能被获取并且未被使用;您可以尝试使用posts_per_page=1
或post_limits=1
,看看是否解决问题。