我只需对标记为粘贴的帖子进行分页。不需要显示非粘性帖子。
我在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();
}
但是分页不能正常工作,我在/ page / 1 /上得到了一个已经发送错误的标题,但在帖子索引页面上它并没有这样做,而在/ page / 2 /它也没有给我这个。此外,它还会抛出prev / next的额外回声,这很奇怪。
答案 0 :(得分:1)
如果您不知道帖子在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++;
}
}
}
正如您所看到的,如果我们将正确的参数传递给WP_Query
并且is_home()
返回true,WP_Query
将返回所有粘性帖子,无论如何。使用post__in
参数时会发生这种情况。这意味着,如果您只是有条件地查询粘贴帖子,无论您作为参数传递什么,您都将在第一页上回复所有粘性帖子。要阻止这种情况发生,我们需要将ignore_sticky_posts
设置为1
,以便我们避免查询所有粘性帖子。
就在post__in
的笔记上。在将任何内容传递给post__in
之前,您应始终确保拥有有效的帖子ID数组。如果你传递一个空数组,就像没有粘贴帖子一样,所有的帖子都会被返回,它不会像你期望的那样返回一个空数组,所以要小心这个
让我们看一下可能的解决方案(未经测试,需要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();
}