简体   繁体   English

WordPress中仅粘性帖子的分页

[英]Pagination for only Sticky Posts in WordPress

I need to have pagination only for posts marked as sticky. 我只需要对标记为粘性的帖子进行分页。 No non-sticky posts need to be shown. 无需显示非粘性帖子。

I had this in the functions.php file 我在functions.php文件中有这个

function sticky_pagination()
{
    //global $wp_query;
    $big = 999999999;

    $args = array('post_type' => 'post', 'posts_per_page' => 5, 'post__in'  => get_option( 'sticky_posts' ));

    $wp_query = new WP_Query($args);

    echo paginate_links(array(
        'base'    => str_replace($big, '%#%', get_pagenum_link($big)),
        'format'  => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total'   => $wp_query->max_num_pages
    ));

    wp_reset_postdata();
}

But pagination doesnt work right and I get a headers already sent error on /page/1/ but on the posts index page it doesnt do that and on /page/2/ it doesnt give me that either. 但是分页无法正常工作,我在/ page / 1 /上收到了已发送标题的错误消息,但在职位索引页面上却没有这样做,在/ page / 2 /上也没有给我发送错误消息。 Plus it dumps out an extra echo of the prev/next which is wierd. 另外,它还丢弃了上级/下级的额外回声。

Querying only certain sticky posts or trying to page a list of sticky posts can be tricky if you do not know how sticky posts work within the WP_Query class. 如果您不知道WP_Query类中的粘性帖子如何工作,则仅查询某些粘性帖子或尝试对粘性帖子列表进行分页可能会很棘手。

Lets quickly look at how stickies are queried inside the WP_Query class 让我们快速查看如何在WP_Query类中查询WP_Query

// Put sticky posts at the top of the posts array
$sticky_posts = get_option('sticky_posts');
if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
    $num_posts = count($this->posts);
    $sticky_offset = 0;
    // Loop over posts and relocate stickies to the front.
    for ( $i = 0; $i < $num_posts; $i++ ) {
        if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
            $sticky_post = $this->posts[$i];
            // Remove sticky from current position
            array_splice($this->posts, $i, 1);
            // Move to front, after other stickies
            array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
            // Increment the sticky offset. The next sticky will be placed at this offset.
            $sticky_offset++;
            // Remove post from sticky posts array
            $offset = array_search($sticky_post->ID, $sticky_posts);
            unset( $sticky_posts[$offset] );
        }
    }
    // If any posts have been excluded specifically, Ignore those that are sticky.
    if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
        $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
    // Fetch sticky posts that weren't in the query results
    if ( !empty($sticky_posts) ) {
        $stickies = get_posts( array(
            'post__in' => $sticky_posts,
            'post_type' => $post_type,
            'post_status' => 'publish',
            'nopaging' => true
        ) );
        foreach ( $stickies as $sticky_post ) {
            array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
            $sticky_offset++;
        }
    }
}

As you can see, if we pass the correct parameters to WP_Query and is_home() returns true, WP_Query will return all sticky posts regardless. 如您所见,如果我们将正确的参数传递给WP_Query并且is_home()返回true,则WP_Query将返回所有粘性帖子。 This happens when you use the post__in parameter. 当您使用post__in参数时,会发生这种情况。 What this means is, if you only query sticky posts conditionally, you will always get all sticky posts back on page one regardless of what you passed as arguments. 这意味着,如果仅有条件地查询粘性帖子,则无论您作为参数传递什么,都将始终将所有粘性帖子返回到第一页。 To stop this from happening, we need to set ignore_sticky_posts to 1 in order for us to avoid querying all sticky posts. 为了阻止这种情况的发生,我们需要将ignore_sticky_posts设置为1 ,以便避免查询所有粘性帖子。

Just on the note of post__in . 就在post__in的音符上。 You should always make sure you have a valid array of post id's before passing anything to post__in . 在将任何内容传递给post__in之前,您应始终确保自己具有一个有效的post id数组。 If you pass an empty array, like when there is no sticky posts, all posts are returned regardless, it does not return an empty array as one would expect, so be careful of this 如果您传递一个空数组(如没有粘性帖子时),则无论是否返回所有帖子,它都不会像预期的那样返回一个空数组,因此请注意

Lets look at a possible soltion ( Untested and requires PHP 5.4+ ) 让我们看一下可能的解决方案( 未经测试,需要PHP 5.4+

// Get all sticky posts
$stickies = get_option( 'sticky_posts' );
// Make sure we have sticky posts
if ( $stickies ) {
    // We have stickies, lets set our query
    $args = [
        'post__in'            => $stickies,
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => 5,
        'paged'               => get_query_var( 'paged', 1 ),
        // Any other arguments
    ];
    $q = new WP_Query( $args );

    // YOUR LOOP
    while ( $q->have_posts() ) {
        $q->the_post();

            // YOUR OUTPUT

    }

    /**
     * Lets use my pagination function 
     * @link http://wordpress.stackexchange.com/a/172818/31545
     */
    if ( function_exists( '' ) ) {
        $paging_args = [
            'query'            => $q
        ];
        echo get_paginated_numbers( $paging_args );
    }

    wp_reset_postdata();
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM