5

我的博客侧边栏search form 有一个简单的,它将仅搜索.blogs

<form action="<?php echo get_site_url() ?>" method="GET">
   <input type="search" name="s" placeholder="Click to Search" class="fld-search" required/>
   <input type="hidden" name="post_type" value="post">
   <button class="btn-search"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>

这是网站网址:http ://dev.wonder.lk/blog/

以下是我应用的搜索类型,

  1. 博客中可用的值 - 对于某些值,它会给出结果,但有时会显示未找到。例如:搜索hello(但您可以Hello World在存档中看到该博客)
  2. 搜索其他post_type值 - 结果是空白页,即使我看不到页眉或页脚。例如:搜索ninja这是一种产品类型

这些是代码,

搜索.php

<?php
while ( have_posts() ) : the_post();
   if(isset($_GET['post_type'])) {
           $type = $_GET['post_type'];
           if($type == 'product') {
              get_template_part( 'woocommerce/archive', 'product' ); //working fine
           } else {
              get_template_part( 'framework/template-parts/page/search', 'post' );
           }
   } else {
           get_template_part( 'framework/template-parts/page/search', 'post' );
   }
   endwhile;
?>

搜索-post.php

<?php

get_header();

//Page Title Bar
$pageTitle = 'Search results for: "'.get_search_query().'"';
echo page_title_bar( $pageTitle, get_template_directory_uri().'/framework/assets/images/pg-title-bar.jpg');
?>

<div class="container blog-wrapper page-container">
    <div class="row">
        <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <?php
                    // Include Blog Posts List
                    get_template_part('framework/template-parts/post/blog', 'post-list');
                ?>

            <?php endwhile; ?>

            <div class="pagination-wrapper">
                <?php pagination(); ?>
            </div>

            <?php else: ?>
                <h3>No results found for: '<?php echo get_search_query(); ?>'</h3>
            <?php endif; ?>
        </div>

        <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
            <?php
                // Include Blog Sidebar
                get_template_part('framework/template-parts/post/blog', 'sidebar');
            ?>
        </div>
    </div>
</div>


<?php get_footer(); ?>

blog-sidebar并且blog-post-list是 HTML 结构。

问我是否需要更多详细信息。

4

2 回答 2

2

search.php更新以下内容后它工作正常,

<?php

get_header();

//Page Title Bar
$pageTitle = 'Search results for: "'.get_search_query().'"';
echo page_title_bar( $pageTitle, get_template_directory_uri().'/framework/assets/images/pg-title-bar.jpg');
?>

<div class="container blog-wrapper page-container">
    <div class="row">
        <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <?php
                    // Include Blog Posts List
                    get_template_part('framework/template-parts/post/blog', 'post-list');
                ?>

            <?php endwhile; ?>

            <div class="pagination-wrapper">
                <?php pagination(); ?>
            </div>

            <?php else: ?>
                <h3>No results found for: '<?php echo get_search_query(); ?>'</h3>
            <?php endif; ?>
        </div>

        <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
            <?php
                // Include Blog Sidebar
                get_template_part('framework/template-parts/post/blog', 'sidebar');
            ?>
        </div>
    </div>
</div>


<?php get_footer(); ?>

如果我得到一个合理且描述清楚的答案,我可以为该答案提供赏金。

于 2018-12-24T08:27:55.873 回答
0

Wordpress 具有内置功能,可为您提供搜索表单

 <?php 
    get_search_form();
 ?>

https://developer.wordpress.org/reference/functions/get_search_form/

于 2018-12-21T13:28:52.920 回答