<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$sticky_posts = get_option('sticky_posts');
if ($paged == 1 && !empty($sticky_posts)) {
rsort($sticky_posts);
query_posts(array('post__in' => $sticky_posts));
while (have_posts()) {
the_post();
get_template_part('post');
}
wp_reset_query();
}
query_posts(array('post__not_in' => $sticky_posts, 'paged' => $paged));
if (have_posts()) {
while (have_posts()) {
the_post();
get_template_part('post');
}
}
wp_reset_query();
?>
或
<?php
$sticky_posts = get_option('sticky_posts');
if (!get_query_var('paged') && !empty($sticky_posts)) {
rsort($sticky_posts);
query_posts(array('post__in' => $sticky_posts));
while (have_posts()) {
the_post();
get_template_part('post');
}
wp_reset_query();
}
query_posts(array_merge(array('post__not_in' => $sticky_posts), $wp_query->query));
if (have_posts()) {
while (have_posts()) {
the_post();
get_template_part('post');
}
}
wp_reset_query();
?>
第二种写法中须注意:
$args = array('post__in' => $sticky_posts);
$args = array_merge($args, $wp_query->query); //此行必需,否则分页失效
query_posts($args);