2014-10-29 84 views
-1

所以我有以下通过邮政类型过滤器吐出,但我怎么可以使用类别或自定义字段?基于类别的搜索结果

$categories = array(
     'post' => 'Articles', 
     'mobile-experience' => 'Mobile', 
     'staff-profiles' => 'Staff Profiles', 
     'events' => 'Events', 
     'page' => 'Page Content',); 

我只是想改变wordpress搜索结果页面来填充它们的类别。

EG。搜索关键字汽车。

类别1

汽车(此类别中找到汽车项目)

类别2

汽车(汽车项目此类别中找到)

类别3

未找到(此类别中没有发现汽车项目)

通过关键字搜索车。

会抓住任何与关键字汽车和类别下填充(项目进行了下2个分类发现关键字汽车,而不是3;到处在那里发现将填充)

基本上而不是拍摄出与关键词汽车所有项目;它会显示搜索到的关键字汽车类别下的结果。

我只有3个类别。

最近尝试(尝试2种)

<?php 
get_header(); ?> 
<div class="content"> 
      <?php 
       $s = get_search_query(); 
      ?> 
    <div class="search"> 
     <div class="categoryThumbs"> 
     <?php if (have_posts()) : ?> 
      <h3><?php printf(__('Search Results for: %s'), '<span>' . get_search_query() . '</span>'); ?></h3> 
     <?php endif;?> 
      <?php query_posts("s='$s'&category_name=build"); ?> 
       <?php if (have_posts()) : ?> 
        <?php $blogResults=0; ?> 
       <?php while (have_posts()) : the_post(); ?> 
        <?php 
         $blogResults++; 
        ?> 
       <?php endwhile; ?> 
        <h4><?php echo $blogResults; ?> Results in BLOG</h4> 
        <?php while (have_posts()) : the_post(); ?> 
        <div class="films"> 
         <div class="thumb"> 
          <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> 
         </div> 
         <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> 
         <div class="entry"> 
          <?php the_excerpt() ?> 
         </div> 
        </div> 
        <?php endwhile; ?> 
       <?php endif;?> 
       <?php query_posts("s='$s'&category_name=apps"); ?> 
       <?php if (have_posts()) : ?> 
        <?php $blogResults=0; ?> 
       <?php while (have_posts()) : the_post(); ?> 
        <?php 
         $blogResults++; 
        ?> 
       <?php endwhile; ?> 
        <h4><?php echo $blogResults; ?> Results in Films</h4> 
        <?php while (have_posts()) : the_post(); ?> 
        <div class="films"> 
         <div class="thumb"> 
          <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> 
         </div> 
         <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> 
         <div class="entry"> 
          <?php the_excerpt() ?> 
         </div> 
        </div> 
        <?php endwhile; ?> 
       <?php endif;?> 
     <div class="spacer"></div> 
     </div> 
    </div> 
</div> 
<?php get_footer(); ?> 
+0

您是否在考虑'array_merge()'也许?你有没有一个你想要做什么的例子? – Rasclatt 2014-10-29 21:35:26

回答

0

只需使用jQuery添加一个隐藏字段搜索表单,

每个类别都有自己独特的体类,你可以使用这个类只为不同类别添加不同的隐藏字段,

你可以使用jQuery前置或后置,

(function($){ 

    //First Category 
    $('.your-category-body-class .your-search-form-class').prepend('<input type="hidden" id="foo" name="category_name" value="your-category-slug" />'); 

    //Second Category 

    $('.your-second-cat-body-class .your-search-form-class').prepend('<input type="hidden" id="foo" name="category_name" value="your-second-cat-slug" />') 

})(jQuery) 

WordPress默认的形式是这样的

<form method="get" class="search-form" action="http://your-site.com/" role="search"> 
    <input type="search" name="s" placeholder="Search Movie..."> 
    <input type="submit" value="Search"> 
</form> 

你也可以这样做http://your-site.com/?post_type=your-custom-post&category_name=your-category-slug&post_tag=your-tag

网址查询时,当你做一个搜索,它会返回这样http://your-site.com/?s=Your+Search+Phrase

的URL,你的目标是要在搜索结果中添加预定义的附加参数返回像http://your-site.com/?s=Your+Search+Phrase&category_name=category-slug

所以,你需要你的搜索形式是这样的

<form method="get" class="search-form" action="http://your-site.com/" role="search"> 
    <input type="search" name="s" placeholder="Search Movie..."> 
    <input type="hidden" id="foo" name="category_name" value="your-category-slug" /> 
    <input type="submit" value="Search"> 
</form> 

,你可以做到这一点通过我上面提供了jQuery,你只需要得到正确的选择

或者简单地创建自己的搜索形式

function my_custom_search_form() { 
    echo '<form method="get" class="search-form" action="http://your-site.com/" role="search">'."\n"; 
    echo '<input type="search" name="s" placeholder="Search Movie...">'."\n"; 
    if(is_category('your-category-slug-or-id')) { 
     echo '<input type="hidden" id="foo" name="category_name" value="your-category-slug" />'."\n"; 
    } elseif(is_category('your-second-slug-or-id')) { 
     echo '<input type="hidden" id="foo" name="category_name" value="your-second-slug-or-id" />'."\n"; 
    } 
    echo '<input type="submit" value="Search">'."\n"; 
    echo '</form>'; 
} 

然后只需使用my_custom_search_form()代替WordPress的默认搜索表单

编辑

您可以通过表单上添加隐藏输入字段自定义搜索查询参数,

,如果你想搜索整个数据库

,那么这就是默认的WordPress的搜索框, 返回URL一样,如果你想对某些类别中搜索这个http://your-site.com/?s=Your+Search+Phrase

,那么你需要搜索表单返回这样的网址http://your-site.com/?s=Your+Search+Phrase&category_name=your-category-slug

,如果你想在两个类别进行搜索,那么你需要搜索表单返回URL liek这http://your-site.com/?s=Your+Search+Phrase&cat=1,2其中和是你的类ID,或合并类别蛞蝓http://your-site.com/?s=Your+Search+Phrase&category_name=your-first-category-slug,second-category-slug

您只需要在搜索表单中添加隐藏值以添加适合您需要的参数

+0

内对结果进行分组但是这不会显示包含关键字的两个或所有类别? – 2014-10-30 16:19:24

+0

你在那里,海滩? – 2014-10-30 18:05:54

+0

我已经更新了我的答案 – silver 2014-10-30 18:51:20

0

如果您有其他阵列,假设$ categories2,你可以这样做:

$结果= array_merge($类,$ categories2);

+0

?! Noooooooooooo – 2014-10-29 21:42:31

+0

我已经根据我对编辑之前的问题的理解作了回答,但我不明白你的编辑,你能解释一下吗? – 2014-10-29 21:42:33

+0

我有一个wordpress网站---分类,试图调整搜索结果页面,让结果填入其分类。我不想更改搜索功能,只是允许将结果归入结果页面的类别下。 – 2014-10-29 21:44:06