【发布时间】:2022-01-12 21:59:21
【问题描述】:
所以我试图在博客页面上创建一个搜索表单,以便它只搜索类别为“博客”的帖子 这是我正在使用的搜索表单代码:
<form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="cat" id="blog" value="7" />
<input type="text" value="<?php echo get_search_query(); ?>" name="s" />
<input type="submit" value="Search" />
</form>
这是 search.php 代码:
<?php
/**
* The template for displaying search results pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
*
* @package Scrap
*/
get_header();
?>
<main id="primary" class="site-main">
<?php
$s=get_search_query();
$args = array(
's' =>$s
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
_e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
}
}else{
?>
<h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
<div class="alert alert-info">
<p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
</div>
<?php } ?>
</main><!-- #main -->
<?php
// get_sidebar();
get_footer();
搜索查询时,URL 加载就像它被限制在类别中一样:https://www.scraperapi.com/?cat=7&s=proxies 但它仍然显示来自该类别之外的页面,并且它不会加载与搜索查询匹配的所有博客文章。 搜索表单位于 https://www.scraperapi.com/blog/,但它显示:隐藏在顶部名为“blog-search”的 div 中。
非常感谢您提供的任何帮助!
【问题讨论】:
标签: php wordpress search search-form