两个搜索页面,一个搜索表单

时间:2013-04-19 作者:Adzay

我有一个搜索表单,我想放在多个页面中(它将是不同的标题类型),我通过使用“获取搜索表单”功能来实现这一点。在我的搜索表上,我有两个电台栏目,列出了两种自定义的帖子类型“海报”和“房子”。

我有两个不同的搜索页面为每一个职位类型。我希望它们是分开的,因为我想在导航中添加搜索单独的搜索页面,例如“搜索海报”、“搜索房子”。

是否有办法让搜索表单将用户重定向到“搜索房屋”页面或“搜索海报”,具体取决于提交条目之前选择的单选按钮?。

最好我想在Searcform中保留所有必要的编码。php文件(如果可能)。

2 个回复
最合适的回答,由SO网友:Adzay 整理而成

这是我的解决方案,我使用单选按钮的Onclick属性来更改表单中元素的“动作”。

<form  id="searchme" action="<?php echo site_url(); ?>/postersearch" method="get">
<ul class=" four columns inline-list offset-by-one">
  <li><label for="radio4"><input name="post_type" CHECKED type="radio" id="radio4" onclick="document.getElementById(\'searchme\').action=\'<?php echo site_url(); ?>/postersearch\'; document.getElementById(\'searchsubmit\').value=\'Search For Posters\';"/> Events</label></li>
  <li><label for="radio5"><input name="post_type"  type="radio" id="radio5" onclick="document.getElementById(\'searchme\').action=\'<?php echo site_url(); ?>/housesearch\'; document.getElementById(\'searchsubmit\').value=\'Search For Houses\';"/> Locations</label></li>
</ul>
<input id="searchsubmit" type="submit" value="Search For posters"  style="width:100%"/>             
</form>

SO网友:fuxia

钩入\'template_include\' 并在此处更改模板。

合成示例,未测试:

add_action( \'template_include\', \'wpse_96472_search_template\' );

function wpse_96472_search_template( $template )
{
    if ( ! is_search() )
        return $template;

    if ( empty ( $_GET[\'post_type\'] ) )
        return $template;

    if ( \'poster\' === $_GET[\'post_type\'] )
        return get_template_directory() . \'/poster-search-template.php\';

    if ( \'house\' === $_GET[\'post_type\'] )
        return get_template_directory() . \'/house-search-template.php\';

    return $template;
}
您必须更改模板名称和$_GET 当然是参数名称。

结束

相关推荐

Search posts by Tag

我希望我的搜索表单只显示按标记组织的结果。我知道你可以添加&tag=TAGNAME 但如何将其集成到表单中,以便我的网站只搜索包含与搜索框中输入的内容相同的标记的帖子?我在页面上有两个搜索,因此理想情况下,我希望能够通过HTML将其添加到表单中,而不是其他任何地方,但任何答案都可以:)谢谢