【发布时间】:2014-05-20 19:31:54
【问题描述】:
我正在使用以下查询在 Wordpress 中显示 4 个最新的置顶帖子。
<?php
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 4 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
ALL OF MY OUTPUTTED CODE GOES HERE - EDITED OUT TO SAVE SPACE
<?php endwhile;?>
<?php } else { echo ""; }?>
<?php wp_reset_query(); ?>
这很好用,但是如果我有一个预定的置顶帖(在未来的某个日期出现),查询会忽略它作为置顶帖之一,只显示 3 - 而不是它应该显示的 4?
如何修改以下代码以确保不显示预定的置顶帖,并且我仍然保留 4 个置顶帖?
下面的更新代码显示所有帖子 - 不仅仅是最近的 4 个粘性帖子。
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'paged' => 1,
'ignore_sticky_posts' => 1
);
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
ALL OF MY OUTPUTTED CODE GOES HERE - EDITED OUT TO SAVE SPACE
<?php endwhile;?>
<?php } else { echo ""; }?>
<?php wp_reset_query(); ?>
【问题讨论】:
标签: php wordpress loops sticky