1

我有这个查询:

<?php $wp_query = array(
    'post__not_in' => array(4269),
    'post_type' => 'whatson',
    'exclude' => '4269',
    'posts_per_page' => 5,
    'order' => 'ASC',
    'orderby' => 'date',
    'post_status' =>array('future','published'));
?>

它目前正在按顺序显示即将发布的帖子...如何在顶部显示粘性帖子,然后在其下方显示其他帖子?

例如,如果两个帖子被标记为置顶,那么它们将显示在顶部,那么其他 3 个帖子将只是即将发布的帖子。

4

2 回答 2

1

不久前我遇到了类似的问题,并设计了以下解决方案。这将向您展示如何输出最多五个帖子,其中置顶的帖子位于顶部。您必须对参数数组进行自己的调整,但这应该为您指明正确的方向。

这是一个确定实际显示了多少粘性帖子的问题,从 5 中减去该数字,然后显示非粘性帖子的余额。

<?php

function define_loop_args($present_cat, $sticky_toggle = 0 ) {

    /*the total number of posts to display*/
    $this->maxNum = 5;

    $loop_args = array(
        'cat' => $present_cat,
    );

    if ( $sticky_toggle == TRUE ) {
        $loop_args['post__in'] = get_option( 'sticky_posts' );
        $loop_args['posts_per_page'] = $this->maxNum;
    } else {
        $loop_args['post__not_in'] = get_option( 'sticky_posts' );
        $loop_args['posts_per_page'] = ((int)($this->maxNum) - (int)($this->sticky_count));
    }

    $this->loop_args = $loop_args;
}


    ?>
<ul class="no-children">
    <?php

    /*
     * STICKY
     *output sticky posts first
     */

    $this->define_loop_args( $catID, 1 );
    /*
     *count the number of sticky posts displayed, in order to calculate how many non-sticky posts to output next
     */
    $sticky_count = 0;

    // The Query
    $the_query = new WP_Query( $this->loop_args );

    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();

        ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php

        $sticky_count++;

    endwhile;
    // End The Loop

    // Reset Post Data
    wp_reset_postdata();


    $this->sticky_count = $sticky_count;

    /*
     * NON-STICKY
     *output non-sticky posts next
     */

    $this->define_loop_args( $catID );

    $the_query = new WP_Query( $this->loop_args );

    while ( $the_query->have_posts() ) : $the_query->the_post();
        ?>

        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>

    <?php

    endwhile;
    // End The Loop

    // Reset Post Data
    wp_reset_postdata();

    ?>
</ul>
于 2013-08-23T18:32:20.913 回答
1

你可以得到它的多重循环如下

<?php
    $sticky = get_option( 'sticky_posts' );

     $args_ordinary = array(
        'post__not_in' => array(4269,$sticky),
        'post_type' => 'whatson',
        'exclude' => '4269',
        'posts_per_page' => 3,
        'order' => 'ASC',
        'orderby' => 'date',
        'post_status' =>array('future','published'));

     $args_sticky = array(
            'posts_per_page' => -1,
            'post__in'  => $sticky,
             'posts_per_page' => 2,
            'post_type' => 'whatson'
        );

    query_posts($args_sticky);
    if (have_posts()): ?>
    <?php while (have_posts()) : the_post(); ?>
      //sticky post
    <?php endwhile; ?>
    <?php endif; ?>

    // Now Ordinary Posts
    query_posts($args_ordinary);
    if (have_posts()): ?>
    <?php while (have_posts()) : the_post(); ?>
      //ordinary post
    <?php endwhile; ?>
    <?php endif; ?>
于 2013-08-23T10:54:17.523 回答