【发布时间】:2016-05-10 17:33:22
【问题描述】:
我设置了一个自定义搜索表单来搜索和显示我的自定义帖子类型的结果。形式为:
<form class="search-form" action="<?php echo home_url( '/' ); ?>">
<input type="search" name="s" placeholder="Search…">
<input type="hidden" name="post_type" value="resource-library">
<input type="submit" value="Search">
</form>
在我的 search.php 中,如果内容类型是资源库,我想将人们引导到一个名为 search-resource-library.php 的自定义搜索 php 文件。所以我把它放在了我的 search.php 页面的顶部:
<?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;
}
?>
我知道这是在做什么,但我似乎无法从应该在搜索表单 html 中设置的 URL 字符串存储帖子类型。
有什么想法吗?
【问题讨论】:
-
“存储”帖子类型是什么意思?你的意思是这行:
$post_type = $_GET['post_type']没有设置帖子类型?还是别的什么? -
正确。单击搜索表单中的提交按钮后,我无法将 post_type 附加到 url。我需要它在 url 中传递一个 ?post_type=research-library。
-
对不起,这更令人困惑。
$post_type在上面的代码中是否获得了正确的值?如果没有,你试过$post_type = $_POST['post_type']; -
不,它没有。 $post_type = $_GET['post_type'];应该在表单代码中的隐藏输入设置的 url 字符串中查找“post_type”的值:。我可能从根本上解决了这个错误。
-
为了进一步阐明这项工作:$post_type = 'research-library';但问题是所有搜索查询都将使用我生成的自定义搜索 php 文件,而不仅仅是研究库。单击搜索表单上的提交后,我需要能够将 url 参数“?post_type=research-library”从搜索表单传递到 url。目前我无法做到这一点。
标签: php wordpress search custom-post-type