$num_posts = count($posts); $sticky_offset = 0; // Loop over posts and relocate stickies to the front. for ( $i = 0; $i < $num_posts; $i++ ) { if ( in_array($posts[$i]->ID, $sticky_posts) ) { $sticky_post = $posts[$i]; // Remove sticky from current position array_splice($posts, $i, 1); // Move to front, after other stickies array_splice($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 ( !emptyempty($sticky_posts) && !emptyempty($wp_query->query_vars[‘post__not_in’] ) ) $sticky_posts = array_diff($sticky_posts, $wp_query->query_vars[‘post__not_in’]); // Fetch sticky posts that weren’t in the query results if ( !emptyempty($sticky_posts) ) { $stickies = get_posts( array( ‘post__in’ => $sticky_posts, ‘post_type’ => $wp_query->query_vars[‘post_type’], ‘post_status’ => ‘publish’, ‘nopaging’ => true ) ); foreach ( $stickies as $sticky_post ) { array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) ); $sticky_offset++; } } }
return $posts; }
代码说明:
1、如果你想让存档页也都显示全部置顶文章,那么就删掉11-43行的代码;
2、如果不想在某分类页显示置顶文章,将第 3 行的
if(
//改成:
// abc是分类名称
if ( is_category( ‘abc’ ) ||