【问题标题】:Query sticky posts only in Wordpress仅在 Wordpress 中查询置顶帖子
【发布时间】:2016-01-14 20:31:45
【问题描述】:

我有以下查询来收集粘性帖子。这在我实际上有粘性帖子时有效,但是当我没有粘性帖子时,它只会查询 10 个(无粘性)帖子。我如何调整查询以仅收集粘性帖子,如果没有找到,则什么也不做。谢谢

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
$the_query2 = new WP_Query( array( 'ignore_sticky_posts' => 'false', 'post__in' => $sticky, 'post_type' => 'ad_listing' ) );
echo $the_query2->post_count;

【问题讨论】:

  • 这里请注意,仅查询置顶帖时,将ignore_sticky_posts设置为true

标签: sticky wordpress


【解决方案1】:

发现加入这个if语句,只查询置顶帖!

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

    if (!empty($sticky)) {
    //Do the query etc.
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    相关资源
    最近更新 更多