【问题标题】:Search pagination wordpress搜索分页wordpress
【发布时间】:2016-01-23 22:46:45
【问题描述】:

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

分页对所有页面有效,仅搜索无效。

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

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    搜索结果通过query_posts 进行实例化,它控制每页有多少结果。在您的代码中,它可能是通过包含发生的:/_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,但会获得类似的结果。

    【讨论】:

    • 不起作用。当我想转到第 2,3 页等时,我仍然会重定向到主页。
    • 如果你想让我为你调试你的代码,你必须摆脱所有这些包含文件。我不知道错误来自哪里。
    • 我编辑了帖子,我关闭了搜索模板包括,我把 item_2.php 放在那里。问题很奇怪,因为在 localhost 和其他网站上运行完美..
    【解决方案2】:

    这是我的分页搜索结果页面代码:

            <?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();后循环之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2011-05-16
      • 1970-01-01
      相关资源
      最近更新 更多