最近碰到一个有趣的问题,在我修改主题时,发现文章页不能正常显示,多出了一篇文章,把应该显示的文章挤到一边去了,这样一来就整个页面形成错位。这是怎么一回事呢?
开始也摸不着头脑。但仔细一想,是在我修改了头文件header.php后才出现这样的问题的。于是我就从这个文件的修改的地方入手。
应客户需要,我在头文件中插入了一段最新文章的代码,代码如下:
<?php query_posts(‘posts_per_page=3&caller_get_posts=1’); ?>
<?php while (have_posts()) : the_post(); ?>
<a target=”_blank” href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>” class=”title”><?php echo cut_str($post->post_title,34); ?></a>
<?php endwhile; ?>
代码是没有错,但是我忘了关闭query_posts()函数,它在文章页single.php中依然起着作用,因single.php中只调用一篇文章,所以它从头部的query_posts()最新文章中调用一篇文章,然后,single.php自己也有一个query_posts(),于是,它又调用了一次,就这样,它调用了2篇文章,出现现在这样的问题。
于是,我试着用wp_reset_query()来结束头部的query_posts() ,问题就解决了,代码如下:
<?php query_posts(‘posts_per_page=3&caller_get_posts=1’); ?>
<?php while (have_posts()) : the_post(); ?>
<a target=”_blank” href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>” class=”title”><?php echo cut_str($post->post_title,34); ?></a>
<?php endwhile; wp_reset_query();?>
The point of view of your article has taught me a lot, and I already know how to improve the paper on gate.oi, thank you. https://www.gate.io/uk/signup/XwNAU