Wordpress搜索模板 - 不显示确切的搜索结果,但显示所有帖子

时间:2018-01-06 15:48:22

标签: php wordpress loops search

我有一个自己的wordpress模板(仍在进行中)。它当然包括search.php模板,如下所示:

<?php

get_header(); ?>

    <section class="row page_intro">
        <div class="row m0 inner">
            <div class="container">
                <div class="row">
                    <h5><?php
                    /* translators: %s: search query. */
                    printf( esc_html__( 'Search Results for: %s', 'vetsandpets' ), '<span>' . get_search_query() . '</span>' );
                ?></h5>
                    <h1><?php _e('News and veterinary advices', 'vetsandpets'); ?></h1>
                </div>
            </div>
        </div>
    </section>

    <section class="row breadcrumbRow">
        <div class="container">
            <div class="row inner m0">
                <?php
                if ( function_exists('yoast_breadcrumb') ) {
                yoast_breadcrumb('
                <p id="breadcrumbs">','</p>
                ');
                }
                ?>
            </div>
        </div>
    </section>

    <section class="row content_section">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-8 blog_list">

                    <?php
                    global $post;
                    setup_postdata( $post );
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

                    query_posts(array(
                        'post_type'      => 'post', // You can add a custom post type if you like
                        'posts_per_page' => '6',
                        'paged'          => $paged
                    ));

                    if ( have_posts() ) : ?>

                    <?php while ( have_posts() ) : the_post(); ?>

                    <div class="row m0 blog blog2">
                        <div class="image_row row m0">

                            <?php the_post_thumbnail('looppostthumbnail', array( 'class' => "img-responsive loop-post-image")); ?>

                        </div>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><h3><?php echo get_the_title(); ?></h3></a>
                        <div class="row m0 meta"><?php _e('Posted on', 'vetsandpets'); ?>: <a href="#"><?php the_time('j F Y'); ?></a></div>
                        <p><?php echo excerpt(50); ?></p>                            
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="view_all"><?php _e('Read more', 'vetsandpets'); ?></a>
                    </div> <!--Single Post-->


                    <?php endwhile; ?>

                        <?php echo wpse247219_custom_pagination(); ?>

                    <?php else : ?>

                        <div class="center"><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></div>

                    <?php endif; wp_reset_postdata(); ?>

                </div>
                <div class="col-sm-12 col-md-4 sidebar">
                    <div class="row m0 widget categories">
                        <h5 class="widget_heading"><?php _e('Categories', 'vetsandpets'); ?></h5>
                        <ul class="list-unstyled">
                            <?php 

                            $args = array(
                                'orderby' => 'count',
                                'depth' => 0,
                                'title_li' => '',
                                'use_desc_for_title' => '',
                                'order' => 'DESC',
                                'hide_empty' => 0
                            );

                                wp_list_categories($args);
                             ?>
                        </ul>
                    </div>
                    <div class="row m0 widget recent_posts">
                        <h5 class="widget_heading"><?php _e('Recent posts', 'vetsandpets'); ?></h5>


                            <?php 
                               // the query
                               $the_query = new WP_Query( array(
                                  'posts_per_page' => 3
                               )); 
                            ?>

                            <?php if ( $the_query->have_posts() ) : ?>
                              <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                                    <div class="media recent_post">
                                        <div class="media-left">
                                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                                <?php the_post_thumbnail('recentpostthumbnail', array( 'class' => "img-responsive recentpostimage")); ?>
                                            </a>
                                        </div>
                                        <div class="media-body">
                                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><h5><?php the_title(); ?></h5></a>
                                            <p><?php _e('Posted on', 'vetsandpets'); ?>: <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_time('j F Y'); ?></a></p>
                                        </div>
                                    </div>                                

                              <?php endwhile; ?>
                              <?php wp_reset_postdata(); ?>

                            <?php else : ?>
                              <p><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></p>
                            <?php endif; ?>

                    </div>
                </div>
            </div>
        </div>
    </section>

<?php
get_sidebar();
get_footer();
?>

就是这样。然后我有一个搜索表单,它总是包含在导航模式中。您可以在下面查看php代码:

    <form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
        <div>
        <input class="form-control" type="search" value="<?php get_search_query(); ?>" id="example-search-input" name="s" id="s" />
        <button type="submit" class="btn searchbtn" id="searchsubmit"><?php _e('Submit', 'vetsandpets') ?></button>
        </div>
        </form>

但它不起作用 - 意思是:它总是显示所有帖子..我做错了什么?它以某种方式链接到此代码/循环中的参数?

1 个答案:

答案 0 :(得分:1)

问题是您通过$post调用重置了全局query_posts()对象。正如WordPress Docs中所述:此函数将完全覆盖主查询,不适合插件或主题使用。其过于简单的修改主查询的方法可能会有问题,应尽可能避免。

所以,你应该删除这些行:

query_posts(array(
    'post_type'      => 'post',
    'posts_per_page' => '6',
    'paged'          => $paged
));

第一个while循环<?php while ( have_posts() ) : the_post(); ?>将遍历搜索结果。