搜索分页 wordpress
[英] Search pagination wordpress
本文介绍了搜索分页 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的搜索结果页面有问题,分页不起作用,当我想转到下一页时,我被重定向到索引页,但网址是:www.mywebsite.com/page/2/?s=word..
分页对所有页面都有效,仅对搜索无效.
这是search.php的代码
<?php$search = isset( $_GET['s'] ) ?$_GET['s']: 空;$user_filter = '';$user_display = '';如果($搜索){$user_filtre .= 's=' .$_GET['s'] .'&';$user_display .= 's=' .$_GET['s'] .'&';}?><div id="main-search" class="content-dark hidden-sm hidden-xs"><div class="容器"><form method="get" action="<?php bloginfo('url'); ?>"接受字符集=UTF-8"><!--input name="s" id="s" type="hidden" --><div id="main-search-fields"><p class="pull-left term">搜索词:</p><input name="s" value="<?php the_search_query(); ?>"autocomplete="off" type="search">
<div id="main-search-btn"><input class="button-green-download-big" type="submit" value="Search">
</表单>
<div class="browse-content"><div class="容器"><部分><div class="row"><ul><?php if(have_posts() ): ?><?php while ( have_posts() ) : the_post();?><?php包含'_includes/items/item_2.php';?><?php endwhile;?><?php endif;?>
</节><?php novavideo_theme_pagination();?>
有人有想法吗?
item_2.php.
<div class="browse-movie-wrap col-xs-10 col-sm-4 col-md-5 col-lg-4"><a href="<?php the_permalink(); ?>"class="浏览电影链接"><图><?php if($values = get_post_custom_values("poster_url")) { ?><img class="img-responsive" src="<?php echo $values[0]; ?>"alt="<?php the_title();?> 在线观看" width="210" height="315"><?php } ?><figcaption class="hidden-xs hidden-sm"><span class="fa fa-star icon-color"></span><h4 class="rating"><?php $rating = get_post_custom_values("imdbRating");回声 $rating[0];?>/10/h4<h4><?php $categories = get_the_category();如果(!空($categories[0])){echo esc_html( $categories[0]->name );}?><h4 class="quality-button"><span class="fa fa-play-circle"></span><?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'quality', '', ', ', '' ) );回声 $terms_as_text;;?></h4><span class="button-green-download-big margin-button">打开电影</span></figcaption></图></a><div class="browse-movie-bottom"><a href="<?php the_permalink(); ?>"class="browse-movie-title"><?php $title = get_post_custom_values("Title");回声 $title[0];?></a><div class="browse-movie-year"><?php $terms = wp_get_post_terms($post->ID, 'release-year', array("fields" => "all"));if ( !empty( $terms ) && !is_wp_error( $terms ) ){ foreach ( $terms as $term_single ) { $term_link = get_term_link( $term_single );echo $term_single->name;?></div>
解决方案
搜索结果通过 query_posts
实例化,并控制每页有多少结果.在您的代码中,它可能通过包含发生:/_includes/search-template.php
但如果没有,请按如下方式设置:
//1- 创建一个分页变量,该变量知道存在多少分页页面(取决于您的站点)$paged = get_query_var('paged') ?get_query_var('分页'): 1;//2- 将 `paged` 传递到 query_posts 的参数数组中$args = 数组('posts_per_page' =>20,'订单' =>'DESC','分页' =>$分页,);//3- 将 query_posts 提交给 WordPress 并设置参数query_posts( $args );
现在您可以使用 next_posts_link();
来显示分页页面链接.只需用 next_post_link();
替换对 theme_pagination();
的引用,它就会输出正确的结果.
如果您想从模板中抽象出所有这些逻辑,请查看 pre_get_posts.这是一种不同的方法,您可以将所有这些逻辑放入 functions.php
中,但会获得类似的结果.
i have a problem with search result page, the pagination doesn't works, when i want to go to next page, i'm redirected to index page, but the url is: www.mywebsite.com/page/2/?s=word..
For all pages the pagination works, only for search doesn't works.
this is the code of search.php
<?php
/**
* The template for displaying Search Results pages.
*/
get_header(); ?>
<?php
$search = isset( $_GET['s'] ) ? $_GET['s']: null;
$user_filtre = '';
$user_display = '';
if( $search ){
$user_filtre .= 's=' . $_GET['s'] . '&';
$user_display .= 's=' . $_GET['s'] . '&';
}?>
<div id="main-search" class="content-dark hidden-sm hidden-xs">
<div class="container">
<form method="get" action="<?php bloginfo('url'); ?>" accept-charset="UTF-8">
<!--input name="s" id="s" type="hidden" -->
<div id="main-search-fields">
<p class="pull-left term">Search Term:</p>
<input name="s" value="<?php the_search_query(); ?>"autocomplete="off" type="search">
</div>
<div id="main-search-btn">
<input class="button-green-download-big" type="submit" value="Search">
</div>
</form>
</div>
</div>
<div class="browse-content">
<div class="container">
<section>
<div class="row">
<ul>
<?php if(have_posts() ): ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php include '_includes/items/item_2.php';?>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</section>
<?php novavideo_theme_pagination(); ?>
</div>
</div>
</div>
Anyone have an ideea?
item_2.php.
<div class="browse-movie-wrap col-xs-10 col-sm-4 col-md-5 col-lg-4">
<a href="<?php the_permalink(); ?>" class="browse-movie-link">
<figure>
<?php if($values = get_post_custom_values("poster_url")) { ?>
<img class="img-responsive" src="<?php echo $values[0]; ?>" alt="<?php the_title();?> Watch Online" width="210" height="315">
<?php } ?>
<figcaption class="hidden-xs hidden-sm">
<span class="fa fa-star icon-color"></span>
<h4 class="rating"><?php $rating = get_post_custom_values("imdbRating"); echo $rating[0]; ?> / 10</h4>
<h4>
<?php $categories = get_the_category();
if ( ! empty( $categories[0] ) ) {
echo esc_html( $categories[0]->name );
}
?>
</h4>
<h4 class="quality-button"><span class="fa fa-play-circle"></span> <?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'quality', '', ', ', '' ) ); echo $terms_as_text;; ?></h4>
<span class="button-green-download-big margin-button">Open Movie</span>
</figcaption>
</figure>
</a>
<div class="browse-movie-bottom">
<a href="<?php the_permalink(); ?>" class="browse-movie-title"><?php $title = get_post_custom_values("Title"); echo $title[0]; ?></a>
<div class="browse-movie-year"><?php $terms = wp_get_post_terms($post->ID, 'release-year', array("fields" => "all")); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term_single ) { $term_link = get_term_link( $term_single ); echo $term_single->name; } } ?></div>
</div>
</div>
解决方案
Search results are instantiated through query_posts
and that controls how many results you have per page. In your code, it's probably happening via the include: /_includes/search-template.php
but if not, set it up like this:
// 1- Created a paged variable that knows how many paginated pages exist (depends on your site)
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// 2- Pass `paged` into array of arguments for query_posts
$args = array(
'posts_per_page' => 20,
'order' => 'DESC',
'paged' => $paged,
);
// 3- Submit query_posts to WordPress with arguments set
query_posts( $args );
Now you can use next_posts_link();
to show the paginated page link. Just replace the references to theme_pagination();
with next_post_link();
and it should output the right results.
If you want to abstract all this logic out of your template, check out pre_get_posts. It's a different method and you would put all this logic into functions.php
but will achieve similar results.
这篇关于搜索分页 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文