![]() |
|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
#1
IP: 112.87.85.4
|
|||
|
|||
|
一般提取分类目录下的文章,都会使用query_posts()函数,这就归功于query_posts()函数的强大,但成也萧何败也萧何,如果大家对query_posts()函数不甚了解的话,就会造成另一个让人头痛的问题:因为使用query_posts()分页功能失效。
先看前面的代码: <h1 class="jiaodianbj"><?php echo single_cat_title( '', false ) ?>&重症医学</h1> <?php query_posts(array('category__and'=>array(get_cat_id(single_cat_title('',false)),71)));?> <?php while (have_posts()) : the_post(); ?> <div class="jdnews"> <ul> <li><span class="grayz">· </span><a href="<?php the_permalink(); ?>" class="black14"><?php echo cut_str($post->post_title,56); ?></a></li> <li class="jdnews_x"><a href="<?php the_permalink(); ?>" class="gray"><?php echo truncate_excerpt(70); ?></a></li> </ul> </div> <?php endwhile;wp_reset_query(); ?> <div class="page_navi"> <?php page_nav($query_string); ?> </div> 如果我们使用了分页插件或自己编写的分页代码,上面的query_posts()函数写法将会导致首页分页功能失效,具体表现为不管大家点击哪一个分页,都只会显示第一页的内容,没有出现我们期待的页面跳转至目标页面的效果。 原因:query_posts()放在LOOP(循环)之前起到限定查询条件的作用,如果你向 query_posts()传递参数,那么wp_query会将你传递的参数生成一个sql查询语句,同时它会忽略来自url的参数,更不会以此为查询依据了,这就造成了一个有趣的现象,但是每个分页内容都是一样的,这就佐证了wp_query生成的查询语句阻止了url参数查询,最终导致了首页分页功能失效。知道了原因,问题就容易解决了。 2个最简洁的解决方法: 1.直接将url的查询参数传入query_posts()函数里 这个查询参数是paged,代码实现如下: “query_posts(“cat=-16,-41,-3&paged=$paged″); ” 或者 // 下面这一行代码是必须的,不然你的首页不能分页 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args =array( // 这里以下面的方式添加query_posts参数,具体参数可以参加官方文档 'orderby' => comment_count, 'paged' => $paged ); query_posts($args); 2.向query_posts()函数传参时使用$query_string,继续接收来自url的参数。 代码实现如下: “query_posts($query_string.”&cat=-3″);” #记住&符号千万不能省略 最后将以上代码作了如下修改: <h1 class="jiaodianbj"><?php echo single_cat_title( '', false ) ?>&重症医学</h1> <?php query_posts(array($query_string.'posts_per_page=-1','category__and'=>array(get_cat_id(single_cat_title('',false)),71)));?> <?php while (have_posts()) : the_post(); ?> <div class="jdnews"> <ul> <li><span class="grayz">· </span><a href="<?php the_permalink(); ?>" class="black14"><?php echo cut_str($post->post_title,56); ?></a></li> <li class="jdnews_x"><a href="<?php the_permalink(); ?>" class="gray"><?php echo truncate_excerpt(70); ?></a></li> </ul> </div> <?php endwhile; wp_reset_query();?> <div class="page_navi"> <?php page_nav($query_string); ?> </div> 这样就解决了分页功能不起作用的问题了 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple Steps to Change Your Table Prefix in WordPress | Abby | WordPress | 0 | 2014-04-14 03:40 PM |
| Install WordPress on IIS | topvip | Blog(博客)/Wiki(维客)/RSS/Cms | 1 | 2010-01-25 08:31 PM |
| 让wordpress首页显示特殊页面(flash首页等) | yahoo | Blog(博客)/Wiki(维客)/RSS/Cms | 0 | 2009-02-25 06:58 AM |
| wordpress 搜索引擎优化的二十条实用技巧 | car | 搜索引擎优化 | 0 | 2008-03-11 08:33 AM |
| WordPress 中文包 | sunshine | Blog(博客)/Wiki(维客)/RSS/Cms | 0 | 2007-03-12 10:16 AM |