Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自定义帖子类型的Wordpress自定义搜索页面_Wordpress_Custom Post Type - Fatal编程技术网

自定义帖子类型的Wordpress自定义搜索页面

自定义帖子类型的Wordpress自定义搜索页面,wordpress,custom-post-type,Wordpress,Custom Post Type,我有一个自定义的post类型recipes,它使用archive recipes.php模板在站点上显示一个分页的食谱列表 我需要能够使用页面顶部的搜索表单搜索这些食谱: <form role="search" method="get" class="searchbox" action="/recipes/"> <input type="text" class="textbox strong" name="s" id="s" value="" placeholder="

我有一个自定义的post类型
recipes
,它使用
archive recipes.php
模板在站点上显示一个分页的食谱列表

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

<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>

执行搜索后是否可以返回此配方列表页面,并以与列表相同的样式显示结果

我似乎找不到任何能让我为自定义帖子类型创建自定义搜索页面的东西


谢谢您的帮助。

您可以使用“模板包含”过滤器

e、 在你的函数文件中

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');

这应该检查对“recipes”自定义帖子类型的搜索,并使用您的存档页面模板搜索结果。

谢谢@NoiseMask。看起来它应该像我需要的那样工作,但是在调试$wp_查询时,我注意到sql请求包含对所有post类型的搜索,而不仅仅是“recipes”。有什么想法吗?nvm,我使用的WP根框架把重写搞砸了。再次感谢。上述解决方案效果良好;但是我发现
get\u query\u var('post\u type')
返回了一个包含所有已注册post类型的数组。相反,我会使用
$post_type=get_post_type()。另外,为了保存对多个post类型的一些条件检查:
return locate_template('archive-'.$post_type..php')