自定义文章类型的WordPress自定义搜索页面

问题描述:

我有一个自定义文章类型recipes,它使用archive-recipes.php模板在网站上显示食谱的分页列表。自定义文章类型的WordPress自定义搜索页面

我需要能够在页面的顶部使用搜索表单通过这些食谱搜索:

<form role="search" method="get" class="searchbox" action="/recipes/"> 
    <input type="text" class="textbox strong" name="s" id="s" value="" placeholder="Search..." /> 
    <input type="hidden" name="post_type" value="recipes" />      
    <button type="submit" class="button icon"></button> 
</form> 

是否有可能回到这个配方列表页面一旦搜索已经执行和显示结果与列表相同?

我似乎无法找到任何能够让我为自定义帖子类型创建自定义搜索页面的任何内容。

感谢您的帮助。

您可以使用'template_include'过滤器。

例如在你的函数文件中。

function template_chooser($template) 
{ 
    global $wp_query; 
    $post_type = get_query_var('post_type'); 
    if($wp_query->is_search && $post_type == 'recipes') 
    { 
    return locate_template('archive-recipes.php'); 
    } 
    return $template; 
} 
add_filter('template_include', 'template_chooser'); 

这应该检查“食谱”自定义帖子类型的搜索并使用您的归档页面模板作为结果。

+0

谢谢@noisymask。看起来它应该像我需要的工作,但是当调试** $ wp_query **我注意到,sql请求包含搜索所有帖子类型,而不仅仅是'食谱'。有任何想法吗? – dclawson

+0

nvm,我使用的WP Roots框架搞乱了重写。再次感谢。 – dclawson

+1

上述解决方案效果很好;但是我发现'get_query_var('post_type')'返回了一个包含所有注册后类​​型的数组。相反,我会使用'$ post_type = get_post_type();'。另外,为了保存一些条件检查多个帖子类型:'return locate_template('archive-'。$ post_type。'.php');' – indextwo