问题
I have a simple search form
on my blog sidebar which will search only the 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>
This is the website url : http://dev.wonder.lk/blog/
Following are the search types that I applied,
- Value that available in blog - For some values it gives the result, but sometimes it says not found. Ex : Search
hello
(but you can see theHello World
blog is there on the archive) - Search someother
post_type
values - Result is a blank white page, even I can't see the header or footer. Ex : Searchninja
It's a product type
These are the codes,
search.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;
?>
search-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
and blog-post-list
are the HTML structure.
Ask me if you want more details.
回答1:
It worked fine after updating the search.php
as following,
<?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(); ?>
If I get a reasonable and well described answer I can offer the bounty for that answer.
回答2:
Wordpress has a built in function to give you a search form
<?php
get_search_form();
?>
https://developer.wordpress.org/reference/functions/get_search_form/
来源:https://stackoverflow.com/questions/53885253/wordpress-search-bar-result-a-blank-white-page