我在页面中有三种不同的搜索形式。产品,餐厅和网页是不同的帖子类型。试图通过隐藏的输入类型在search.php中显示结果。它没有显示出我期望的结果。我的代码中的某些内容是错误的。请提供建议或建议。Wordpress搜索三种不同的形式
形式1
<form role="search" method="get" action="<?php bloginfo('url'); ?>">
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="Restaurant" name="post_type" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
形式2
<form role="search" method="get" action="<?php bloginfo('url'); ?>">
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="Products" name="post_type" />
<input type="submit" id="searchsubmit" value="Search" />
</form>
形式3
<form role="search" method="get" action="<?php bloginfo('url'); ?>">
<input type="text" name="search" placeholder="Search...">
<input type="hidden" name="post_type" value="Pages" />
</form>
以下是我的search.php结构
$myvalue = $_GET['post_type'];
if ($myvalue == "Restaurant"){
echo "res";
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'sanha-restau');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
if (have_posts()) :
while (have_posts()) : the_post();
echo '<ul><li><strong>';
echo get_the_title() . '</strong><br> (';
echo substr(get_the_excerpt(), 0, 200) . ') ';
echo '</li></ul>';
endwhile;
else :
get_template_part('content', 'none');
endif;
} else if ($myvalue == "Products"){
echo "pro";
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'sanha-product');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
if (have_posts()) :
while (have_posts()) : the_post();
echo '<ul><li><strong>';
echo get_the_title() . '</strong><br> (';
echo substr(get_the_excerpt(), 0, 200) . ') ';
echo '</li></ul>';
endwhile;
else :
get_template_part('content', 'none');
endif;
} else if ($myvalue == "Pages"){
echo "pages";
if (have_posts()) :
while (have_posts()) : the_post();
echo '<ul><li><strong>';
echo get_the_title() . '</strong><br> (';
echo substr(get_the_excerpt(), 0, 200) . ') ';
echo '</li></ul>';
endwhile;
else :
get_template_part('content', 'none');
endif;
}
请指教。感谢
请提供您的反馈,如果你找到答案非常有帮助:) – Noman