Wordpress:自定义分类的搜索表单(通过输入字段不下拉)

时间:2014-07-07 09:53:23

标签: wordpress .htaccess search customization custom-taxonomy

我制作了自定义分类页:aamir khan,salman khan,aab cc ..等等 因此它的链接是:

  

本地主机/ myproject的/?运动=阿米尔汗

.. llly for others。

现在我想要自定义搜索表单:其中任何一个搜索aamir khan ..它应该显示相同的自定义分类页面aamir khan.below是我的自定义搜索表单:

<form role="customsearch" method="get" id="customsearchform" action="<?php echo home_url('/'); ?>">
<div>
<label for="s">Search for:</label>
<input type="text" value="" name="motion" id="motion" />
<input type="submit" id="customsearchsubmit" value="Search Motion" />
</div>
</form>

但是当我在上面的搜索表单中搜索aamir khan时,它会显示“找不到页面”

生成的链接是

  

本地主机/ myproject的/?运动=阿米尔+汗

我知道aamir khan在搜索“GET”中将被视为不同的单词..因此它显示“+”......

因此,什么是最好的方式..所以我可以搜索分类数据和结果展望应该与我的自定义分类页面相同...

注意:我没有使用自定义帖子......

我也希望输入字段自定义搜索自定义分类...而不是下拉搜索..

1 个答案:

答案 0 :(得分:1)

下面给出的解决方案仅适用于WordPress的当前配置。

通常,人们会按术语名称搜索而不是slug。因此,我们必须从最终用户输入的值中找出slug,然后WordPress将显示该术语的内容。

在functions.php文件中添加以下代码

add_action('init', 'wdm_change_motion_slug');

function wdm_change_motion_slug()
{
    if(isset($_GET['motion']) && !empty($_GET['motion']))
    {
            $term = term_exists($_GET['motion'], 'motion'); //Check if the term already exists

            if ($term !== 0 && $term !== null)
            {
                $_GET['motion'] = get_term_by('id', (int)$term['term_id'], 'motion')->slug; //Find out slug of term from the term id
            }
    }
}

如果它解决了这个问题,请告诉我。