WordPress自定义帖子类型搜索结果页面

WordPress自定义帖子类型搜索结果页面

本文介绍了WordPress自定义帖子类型搜索结果页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个自定义搜索表单,以搜索和显示我的自定义帖子类型的结果。形式为:

I set up a custom search form to search and display results of my custom post type. The form is:

<form class="search-form" action="<?php echo home_url( '/' ); ?>">
  <input type="search" name="s" placeholder="Search&hellip;">
  <input type="hidden" name="post_type" value="resource-library">
  <input type="submit" value="Search">
</form>

在我的search.php中,我想将人们引导到一个名为search-resource-的自定义搜索php文件中如果内容类型是资源库,则为library.php。所以我把它放在search.php页面的顶部:

In my search.php I want to direct folks to a custom search php file called search-resource-library.php if the content type is resource-library. So I put this at the top of my search.php page:

<?php
// store the post type from the URL string
$post_type = $_GET['post_type'];
// check to see if there was a post type in the
// URL string and if a results template for that
// post type actually exists
if ( isset( $post_type ) && locate_template( 'search-' . $post_type . '.php' ) ) {
  // if so, load that template
  get_template_part( 'search', $post_type );

  // and then exit out
  exit;
}
?>

我完全知道这是在做什么,但是我似乎无法从URL中存储帖子类型

I get exactly what this is doing but I can't seem to store the post type from the URL string supposedly set in the search form html.

有什么想法吗?

推荐答案

我发现了问题。我的原始代码和方法是正确的。由于已安装名为 Nice Search的插件,因此未从搜索表单传递url参数。此插件将?s = query搜索重定向到/ search / query,并将%20转换为+。

I found the issue. My original code and method is correct. The url parameters were not being passed from the search form because a plugin called "Nice Search" was installed. This plugin redirects ?s=query searches to /search/query, and converts %20 to +.

这篇关于WordPress自定义帖子类型搜索结果页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:37