分页不适用于搜索结果模板

时间:2013-07-18 作者:RoseCoder

我似乎无法在搜索结果模板中正确分页。页面链接出现了,但当我点击第2页时,它给了我一个404错误。

Search.php

<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>

<?php
if(!$wp_query) global $wp_query;
$search_refer = $_GET["post_type"];
if ($search_refer == \'archive\') { load_template(TEMPLATEPATH . \'/search-gallery.php\');}
else{load_template(TEMPLATEPATH . \'/search-standard.php\');}
?>

Pagination Code in Functions.php

function pagination($pages = \'\', $range = 4){  
 $showitems = ($range * 2)+1;  

 global $paged;
 if(empty($paged)) $paged = 1;

 if($pages == \'\')
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 }   

 if(1 != $pages)
 {
     echo "<div class=\\"pagination\\"><span>Page ".$paged." of ".$pages."</span>";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>&laquo; First</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo; Previous</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href=\\"".get_pagenum_link($paged + 1)."\\">Next &rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last &raquo;</a>";
     echo "</div>\\n";
 }
}

Search Results Template

    <?php
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $search_query = array(
        \'posts_per_page\' => \'10\',
        \'paged\' => $paged,
        );
    $wp_query = new WP_Query($search_query);

    if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>


<div class="courseContent coursesLayout alumniMainContent alumniSpacing newsBoxMore">
        <div class="courseContentHolder alumniContentHolder newsBoxMainHolder">
            <div class="newsMainImage">
                <?php the_post_thumbnail(); ?>
            </div>

            <div class="alumniHeading newsMainHeading">
                <h5><?php the_title();?></h5>
            </div>

            <div class="alumniContent newsMainContent">
                <?php the_excerpt(); ?> 
            </div>
        </div>
        <span class="readmoreButton" style="display: inline !important;"><a title="Student Awards" href="<?php the_permalink();?>">Read More</a></span>
</div><?php edit_post_link(\'Edit\'); ?>
<?php endwhile; ?><?php else : ?>       
    <p class="notfoundtext">Sorry, nothing came back matching what you searched. Try again?</p>
<?php endif; ?>     

<?php pagination(); ?>
有什么建议吗?

2 个回复
SO网友:Imperative Ideas

关于重复的第1页通常是wp\\U查询问题。404是一个永久性问题-尝试将其设置为默认值,看看是否会出现相同的问题。如果没有,那么将它们切换回自定义,您的问题应该已经自行解决了。

如果默认permalinks在第二页上为您提供了404,那么我们需要更仔细地查看分页代码。就我个人而言,我是贾斯汀·塔洛克(JustinTadlock)代码的粉丝,因为它完美无缺。

https://github.com/justintadlock/hybrid-core/blob/master/extensions/loop-pagination.php

SO网友:s_ha_dum

您正在覆盖$wp_query 在搜索模板内。这意味着在加载搜索模板之前运行的主查询将退出或与$wp_query 在模板中。这往往会导致404。

因为你唯一要做的就是改变posts_per_page a非常简单pre_get_posts 回调将至少修复部分问题。

function alter_search_ppp_wpse_106961($qry) {
  if ($qry->is_main_query() && $qry->is_search()) {
    $qry->set(\'post_per_page\',10);
  }
}
add_action(\'pre_get_posts\',\'alter_search_ppp_wpse_106961\');
有了这个您不需要的地方,并且应该删除,这:

$search_query = array(
    \'posts_per_page\' => \'10\',
    \'paged\' => $paged,
    );
$wp_query = new WP_Query($search_query);
我不知道您为什么要使用自己的分页,我看不出代码是否会产生问题URLs、 但我建议你调查一下paginate_links

结束

相关推荐

Pagination in category

Updated: I have 50 categories and i wants to show each 5 category on one page and next 5 on next page and next 5 on next page. and so on. with pagination.我在WordPress网站上有50个类别。我想在每一页上显示五个类别,用10篇文章分页。我已经尝试过了,但分页并没有产生效果。<?php // get all the categ