我看到两个主要问题。a) 您不应该直接修改全局变量&;b) 你不应该使用query_posts
. 这是一个重新设计的示例
功能。php
add_action(\'pre_get_posts\', \'customize_query\');
function customize_query($query) {
if(!$query->is_main_query() || !is_page_template(\'template-file-name.php\'))
return;
$wp_query = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => 1,
\'category__in\' => 3,
\'post__in\' => get_option( \'sticky_posts\' )
));
$query->set(\'posts_per_page\', -1);
$query->set(\'ignore_sticky_posts\', 1);
if(!empty($wp_query->posts))
$query->set(\'post__not_in\', array($wp_query->posts[0]->ID));
}
在模板文件中
$query = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => 1,
\'category__in\' => 3,
\'post__in\' => get_option( \'sticky_posts\' )
));
while ($query->have_posts()) : $query->the_post();
echo the_title();
echo \'<br />\';
endwhile;
echo \'<br />\';
echo \'<br />\';
while (have_posts()) : the_post();
echo the_title();
echo \'<br />\';
endwhile;