搜索分页wordpress

时间:2016-01-23 22:46:46

标签: wordpress

我的搜索结果页面有问题,分页不起作用,当我想转到下一页时,我被重定向到索引页面,但网址是:www.mywebsite.com /页/ 2 /?S =字..

对于分页的所有页面,只有搜索不起作用。

这是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>    

任何人都有想法吗?

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>

2 个答案:

答案 0 :(得分:0)

搜索结果通过query_posts实例化,并控制每页的结果数量。在您的代码中,它可能通过include:/_includes/search-template.php发生,但如果没有,请将其设置为:

// 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 );

现在,您可以使用next_posts_link();来显示分页页面链接。只需将theme_pagination();的引用替换为next_post_link();,它就会输出正确的结果。

如果您想从模板中抽象出所有这些逻辑,请查看pre_get_posts。它是一种不同的方法,您可以将所有这些逻辑放入functions.php,但会获得类似的结果。

答案 1 :(得分:0)

这是我的带有分页功能的搜索结果页代码:

        <?php get_header(); ?>   
        <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/search.css">    
    <?php wp_head(); ?>
    <script type="text/javascript"> 
       var window_width = window.screen.width;
       var window_height = window.screen.height;
    <?php $window_width = "<script>document.write(window_width)</script>"; 
       $window_height = "<script>document.write(window_height)</script>";    ?> 
    </script>
    <style>
    .sm_bd{padding: 5px;
    border: 1px solid
    #ccc;}    
    .row {
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display:         flex;
      flex-wrap: wrap;
    }
    .row > [class*='col-'] {
      display: flex;
      flex-direction: column;
    }
        .previous,.next {
        padding: 5px;
        border: 1px solid 
        #ccc;
        border-radius: 4px;
        margin: 15px;
    }
    </style>
    </head>
    <body class="index-body">
        <?php include 'menu.php';?>
        <div class="col-xs-12 col-sm-12 blog-page no-marg no-padd">
            <div class="col-xs-4 col-sm-3 single-sidebar no-marg no-padd">
                My sidebar
            </div>

            <section id="primary" class="content-area col-xs-12 col-sm-12 col-lg-8 col-md-8">
                <div id="content" class="site-content row" role="main">

                <?php
                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';
                    $s=get_search_query();
                    $args = array(
                                      's' =>$s,
                                      'paged'                  => $paged,
                                      'posts_per_page'         => '12',
                                );
                        // The Query

                    $the_query = new WP_Query( $args );
                    if ( $the_query->have_posts() ) {
                            _e("<h2 style='font-weight:bold;color:#000'>نتایج جستجو برای: ".get_query_var('s')."</h2>");
                             echo "<div class='col-lg-12 col-md-12'><hr/></div>";
                            while ( $the_query->have_posts() ) {
                               $the_query->the_post();
                                     ?>
                                       <div class="col-xs-12 col-sm-12 col-lg-3 col-md-3 sm_bd">
                                           <?php if ( has_post_thumbnail() ) : ?>
                                                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                                    <?php the_post_thumbnail(); ?>
                                                </a>
                                            <?php  else : ?>
                                             <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                                    <img class="attachment-post-thumbnail size-post-thumbnail wp-post-image lazyloaded" src="Without Image" 
                                                    alt="<?php the_title_attribute(); ?>" 
                                                    title="<?php the_title_attribute(); ?>" width="300" height="449">
                                                </a>
                                            <?php endif;?>
                                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                       </div>
                                     <?php
                            }

                            ?>
                            <div class="pagination" id="blog-pagination">
                              <?php if( get_previous_posts_link('&larr; ', $loop->max_num_pages ) ) : ?>
                              <span class="previous" ><?php previous_posts_link( '&rarr; ', $loop->max_num_pages  ); ?></span>
                             <?php endif; ?>
                             <?php if( get_next_posts_link('&larr; ', $loop->max_num_pages ) ) : ?>
                              <span class="next"><?php next_posts_link( '&larr;', $loop->max_num_pages  ); ?></span>
                              <?php endif; ?>
                            </div>
                             <?php 
                        }
                        else{
            ?>
            <h2 style='font-weight:bold;color:#000'>not found title</h2>
            <div class="alert alert-info">
              <p>not found alert</p>
            </div>
          <?php } 

wp_reset_postdata();?>

                </div><!-- #content .site-content -->
            </section><!-- #primary .content-area -->
        </div>

        <?php get_footer(); ?>

别忘了写wp_reset_postdata();循环后。