我似乎无法在搜索结果模板中正确分页。页面链接出现了,但当我点击第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)."\'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>‹ 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 ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last »</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(); ?>
有什么建议吗?
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);
我不知道您为什么要使用自己的分页,我看不出代码是否会产生问题
URL
s、 但我建议你调查一下
paginate_links