只查询最新的sticky post
Querying only the latest sticky post
所以我试图只从 WordPress 中提取最新的粘性 post,并且只有 1 个。我正在使用 'showpost=1' 但出于某种原因,如果我有两个 post 标记因为粘性都出现了?
<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 代码以获取最新的便签 post:
<?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
来查看是否可以解决问题。
所以我试图只从 WordPress 中提取最新的粘性 post,并且只有 1 个。我正在使用 'showpost=1' 但出于某种原因,如果我有两个 post 标记因为粘性都出现了?
<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 代码以获取最新的便签 post:
<?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
来查看是否可以解决问题。