在顶部显示粘贴帖子,然后显示其他帖子

时间:2013-08-23 10:21:16

标签: wordpress

我有这个问题:

<?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个帖子将只是即将发布的帖子。

2 个答案:

答案 0 :(得分:1)

我有一段时间遇到过类似的问题并设计了以下解决方案。这将向您展示如何输出最多五个帖子,其中粘性帖子位于顶部。您必须自己调整arguments数组,但这应该指向正确的方向。

确定实际显示了多少个粘性帖子,从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>

答案 1 :(得分:0)

你可以通过以下方式获得多个循环

<?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; ?>
相关问题