Show sticky posts at the top, then display other p

2019-05-30 19:11发布

I have this query:

<?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'));
?>

It's currently showing upcoming posts in order... how can I show the sticky posts at the top, then show the other posts below it?

E.g If two posts have been marked as sticky then they would show at the top, then the other 3 posts will just be the upcoming posts.

标签: wordpress
2条回答
看我几分像从前
2楼-- · 2019-05-30 19:47

I had a similar problem a while back and devised the following solution. This will show you how to output a maximum of five posts, with the sticky ones at the top. You will have to make your own adjustments to the arguments array, but this should point you in the right direction.

It's a matter of determining how many sticky posts were in fact displayed, subtracting that number from 5, then displaying the balance of non-sticky posts.

<?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>
查看更多
Bombasti
3楼-- · 2019-05-30 19:51

you can get it ny multiple loop as follows

<?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; ?>
查看更多
登录 后发表回答