Goal/Information

我正在尝试设置自定义搜索字段和自定义搜索结果页面以显示一类页面 . 我需要在单独的自定义搜索结果页面上显示这些结果,以便我可以自定义结果的显示方式,而不会影响默认搜索 .

对于上下文,我正在设置一个用户可以搜索关键字的图库,相关页面将显示为缩略图网格,每个缩略图链接到包含有关图像的更多信息的页面 .

包含图像和信息的每个页面都设置为 images 类别下的页面(不是帖子) .

我'm not sure this is relevant, but in a previous project I'已设置默认搜索以在我的functions.php中使用以下代码排除 images 页面(类别ID为30) . 我've tried removing this code and it didn' t影响我下面描述的问题 .

/* Exclude a Category from Search Results */
add_filter( 'pre_get_posts' , 'search_exc_cats' );
function search_exc_cats( $query ) {
    if( $query->is_admin )
        return $query;
    if( $query->is_search ) {
        $query->set( 'category__not_in' , array( 30 ) ); // Cat ID
    }
    return $query;
}

What I've Tried

我还在学习php,花了很多时间在这上面无济于事 . 最近我尝试从this similar question调整一些代码 .

我得到的自定义搜索表单如下所示:

<form class="search-form" action="<?php echo home_url( '/' ); ?>" method="get">
  <input name="s" type="text" placeholder="Search Images" />
  <input name="post_type" type="hidden" value="page" />
  <input name="category_name" type="hidden" value="images" />
  <input alt="Search" type="submit" value="Search" />
</form>

我已将以下代码添加到search.php的顶部以尝试重定向到另一个结果页面,在本例中为search-page.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;
}
?>

注意:我直接从上面链接的类似问题中获取了这个 .

我已经将search.php复制到search-page.php中,从上面删除了上面的代码 .

Problem

现在让我们在上面的自定义表单中搜索"marble"(一个 image 页面中的一个术语) . URL看起来正确( http://example.com/%3C?s=marble&post_type=page&category_name=images ),但我得到了404.此外,正常搜索(那些不在自定义表单中)现在将我带到白页 - 甚至不是404.当我从functions.php中删除上面的代码时,默认搜索工作正常 . (自定义搜索仍然是404s . )

Other Attempts

我也曾几次从Smashing杂志上阅读这本指南 - Building An Advanced WordPress Search With WP_Query - 但不能完全找出与我相关的信息 .

我已经尝试了其他几十个指南和问题,但其中大部分都涉及单独搜索特定的帖子类型(而不是类别)或从类别的单选按钮中搜索 . 我无法从中汲取任何有效的解决方案 .

对我所尝试的内容有什么调整 - 或完全不同的解决方案 - 这有助于达到预期的效果吗?具体来说, how to create a custom search form for one type of page category, leading to a customizable, separate search results page (independent from the default search results page).

提前致谢 .