我正在尝试在我的网站上创建自定义搜索,除了一个问题之外,一切都很顺利——分页时没有刷新内容。
这就是我正在做的。
我的搜索表单:
<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
<h3><?php _e( \'Search\', \'bmpl\' ); ?></h3>
<input type="hidden" name="search" value="advanced">
<label for="name"><?php _e( \'Keyword: \', \'bmpl\' ); ?></label><br>
<input type="text" value="" placeholder="<?php _e( \'Keyword:\', \'bmpl\' ); ?>" name="name" id="name" />
<input type="submit" id="searchsubmit" value="Go" />
下面是函数中的示例函数,用于在特定模板上加载自定义搜索结果
function wpse_load_custom_search_template(){
if( isset($_REQUEST[\'search\']) == \'advanced\' ) {
require(\'advanced-search-result.php\');
die();
}}add_action(\'init\',\'wpse_load_custom_search_template\');
现在是最后一个搜索结果模板
<div class="main-content-wrap" style="color:white;">
<div class="info-content clearfix">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="row">
<?php
$_name = $_GET[\'name\'] != \'\' ? $_GET[\'name\'] : \'\';
$current_page = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
\'s\' => $_name,
\'posts_per_page\' => 6,
\'paged\' => $current_page
);
$wp_query = new WP_Query( $args );?>
<?php if ($wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="col-md-4 col-sm-6 col-xs-6 col-ts-12">
<a href="<?php echo get_permalink() ?>">
<div class="news-item-card">
<div class="news-item-img" >
<h3><?php the_title(); ?></h3>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<?php kama_pagenavi(\'\', \'\', true, array(), $wp_query); ?>
<?php wp_reset_postdata();?>
<?php else: ?>
<p class="not_found">Nothing found<a href="<?php echo get_home_url(); ?>"> Home</a></p>
<?php endif; ?>
</div>
Kama pagenavi-分页功能,适用于同一网站上的所有非搜索页面,搜索应该在那里工作。分页功能代码:
函数kama\\u pagenavi($之前=“”,$之后=“”,$echo=true,$args=array(),$wp\\u query=null){if(!$wp\\u query){wp\\u reset\\u query();global$wp\\u query;}
$default_args = array(
\'text_num_page\' => \'\',
\'num_pages\' => 10,
\'step_link\' => 10,
\'dotright_text\' => \'…\',
\'dotright_text2\' => \'…\',
\'back_text\' => \'« back\',
\'next_text\' => \'next»\',
\'first_page_text\' => \'« start\',
\'last_page_text\' => \'end»\',
$default_args = apply_filters(\'kama_pagenavi_args\', $default_args );
$args = array_merge( $default_args, $args );
extract( $args );
$posts_per_page = (int) $wp_query->get(\'posts_per_page\');
$paged = (int) $wp_query->get(\'paged\');
$max_page = $wp_query->max_num_pages;
if( $max_page <= 1 )
return false;
if( empty( $paged ) || $paged == 0 )
$paged = 1;
$pages_to_show = intval( $num_pages );
$pages_to_show_minus_1 = $pages_to_show-1;
$half_page_start = floor( $pages_to_show_minus_1/2 );
$half_page_end = ceil( $pages_to_show_minus_1/2 );
$start_page = $paged - $half_page_start;
$end_page = $paged + $half_page_end;
if( $start_page <= 0 )
$start_page = 1;
if( ($end_page - $start_page) != $pages_to_show_minus_1 )
$end_page = $start_page + $pages_to_show_minus_1;
if( $end_page > $max_page ) {
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = (int) $max_page;
}
if( $start_page <= 0 )
$start_page = 1;
$out = \'\';
$link_base = str_replace( 99999999, \'___\', get_pagenum_link( 99999999 ) );
$first_url = get_pagenum_link( 1 );
if( false === strpos( $first_url, \'?\') )
$first_url = user_trailingslashit( $first_url );
$out .= $before . "<div class=\'wp-pagenavi\'>\\n";
if( $text_num_page ){
$text_num_page = preg_replace( \'!{current}|{last}!\', \'%s\', $text_num_page );
$out.= sprintf( "<span class=\'pages\'>$text_num_page</span> ", $paged, $max_page );
}
if ( $back_text && $paged != 1 )
$out .= \'<a class="prev" href="\'. ( ($paged-1)==1 ? $first_url : str_replace( \'___\', ($paged-1), $link_base ) ) .\'">\'. $back_text .\'</a> \';
if ( $start_page >= 2 && $pages_to_show < $max_page ) {
$out.= \'<a class="first" href="\'. $first_url .\'">\'. ( $first_page_text ? $first_page_text : 1 ) .\'</a> \';
if( $dotright_text && $start_page != 2 ) $out .= \'<span class="extend">\'. $dotright_text .\'</span> \';
}
for( $i = $start_page; $i <= $end_page; $i++ ) {
if( $i == $paged )
$out .= \'<span class="current">\'.$i.\'</span> \';
elseif( $i == 1 )
$out .= \'<a href="\'. $first_url .\'">1</a> \';
else
$out .= \'<a href="\'. str_replace( \'___\', $i, $link_base ) .\'">\'. $i .\'</a> \';
}
$dd = 0;
if ( $step_link && $end_page < $max_page ){
for( $i = $end_page+1; $i<=$max_page; $i++ ) {
if( $i % $step_link == 0 && $i !== $num_pages ) {
if ( ++$dd == 1 )
$out.= \'<span class="extend">\'. $dotright_text2 .\'</span> \';
$out.= \'<a href="\'. str_replace( \'___\', $i, $link_base ) .\'">\'. $i .\'</a> \';
}
}
}
// в конец
if ( $end_page < $max_page ) {
if( $dotright_text && $end_page != ($max_page-1) )
$out.= \'<span class="extend">\'. $dotright_text2 .\'</span> \';
$out.= \'<a class="last" href="\'. str_replace( \'___\', $max_page, $link_base ) .\'">\'. ( $last_page_text ? $last_page_text : $max_page ) .\'</a> \';
}
// вперед
if ( $next_text && $paged != $end_page )
$out.= \'<a class="next" href="\'. str_replace( \'___\', ($paged+1), $link_base ) .\'">\'. $next_text .\'</a> \';
$out .= "</div>". $after ."\\n";
$out = apply_filters(\'kama_pagenavi\', $out );
if( $echo )
return print $out;
return $out;
}
但即使使用一些简单的东西,比如下一个\\上一个链接,仍然无法很好地工作。
希望有人能帮助我,因为这个问题让我在第三个网站上连续发疯,无法理解到底出了什么问题,这个问题慢慢让我发疯。
抱歉,篇幅太长,英语不好