【发布时间】:2016-08-25 11:08:26
【问题描述】:
我在一个页面中有三种不同的搜索表单。产品、餐厅和页面是不同的帖子类型。尝试通过隐藏输入类型在 search.php 中显示结果。它没有像我预期的那样显示结果。我的代码中有问题。请建议或建议。
表格 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;
}
请指教。谢谢
【问题讨论】:
-
如果您发现答案有帮助,请提供您的反馈:)
标签: wordpress search custom-post-type