如何根据另一个分类术语过滤分类术语

时间:2014-03-22 作者:Dev

我创建了一个名为“Tour”的自定义帖子类型,其中包含多达五种分类法,分别称为“destination”、“Types”、“Month of Travel”、“Year of Travel”和“Lead by”。

现在,我使用“wp\\u dropdown\\u categories”将它们作为下拉类别(每个分类法都作为下拉列表列出)。搜索词在组合中工作良好(如果存在)。

例如:我正在搜索“destination”作为“Bangalore”和“Month of travel”作为“June”的组合作品,因为帖子是根据特定条件选择的。~太完美了。

但是,当没有为类别选择帖子/没有搜索组合时。。。它会触发“无可用帖子”。~这在word press中很常见。

因此,我更喜欢根据以前的分类法过滤分类法(在关联它们时有点混乱)。

主要概念如下:

如果我选择“目的地”为“班加罗尔”,我需要根据目的地过滤“类型”。也就是说班加罗尔只能有两种类型“A型和B型”,其余类型应忽略。-(这五件事再次成为分类法)。

我想要这样的东西enter image description here

如果post\\U type=巡更(&U);tour\\u destination=班加罗尔使用相关术语过滤其他分类法?

帮帮我~提前谢谢

开发人员

1 个回复
最合适的回答,由SO网友:Dev 整理而成

我对早期的代码做了一些调整,第一个过滤器目前运行良好,我对其余部分有点困惑,有什么建议吗?你能帮我把这个带到有效的地方吗。

<!-- last edit started by dev -->
 <script type="text/javascript">
  function onchangedestination(str)
    {
      var xmlhttp;

      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      } 

      xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
          document.getElementById("data").innerHTML = xmlhttp.responseText;
        }
      }

      xmlhttp.open("GET","http://example.com/ajax?opt="+str, true);
      xmlhttp.send();
    }
</script>
 <select class="tourdestination" name="tourdestination" id="tourdestination" onchange="onchangedestination(this.value);">
 <option value="" selected="selected">All Destinations</option>
    <?php
    foreach( get_categories(\'taxonomy=tour_destination&post_type=tour&hide_empty=false\') as $cat ) :

    if( !$cat->parent ) // if cat info not has parents
    {
        echo \'<option value="\' . $cat->term_id  . \'" >\' . $cat->name . \'</option>\';
        category_tree( $cat->term_id ); 
    }
    endforeach;

    wp_reset_query();

    function category_tree( $cat ) { // passing ids of category
        $args = array(\'category__in\' => array( $cat ));
        $next = get_categories(\'taxonomy=tour_destination&hide_empty=false&parent=\' . $cat);

        if( $next ) :
            foreach( $next as $cat ) :
                echo \'<option value="\' . $cat->term_id  . \'" >&nbsp;\' . $cat->name.\'</option>\';
                category_tree( $cat->term_id );
            endforeach;
        endif;
    }
    ?>
 </select>

<div id="data"></div>
文件测试。php

<?php
  $opt = $_GET[\'opt\'];
  $pages = get_posts(array(
                 \'post_type\' => \'tour\',
                 \'numberposts\' => -1,
                 \'tax_query\' => array(
                    array
                    (
                      \'taxonomy\' => \'tour_destination\',
                      \'field\' => \'id\',
                      \'terms\' => $opt, // above code will return the id here.
                      \'include_children\' => false
                    )
                    )
                    ));


    $post_id =array();
    //$tour_type = array();
    $categories = array();
    foreach ($pages as $page) 
        { 
            $post_id[] = $page->ID;
        }
?>
 <?php //echo $count = count($post_id) . \'Total Posts <br>\'; ?>
 <?php foreach($post_id as $p_ID): ?> 
 <?php $tours = wp_get_post_terms($p_ID, \'tour_type\', array("fields" => "all")); ?> 
 <?php $month_travels = wp_get_post_terms($p_ID, \'month_travel\', array("fields" => "all")); ?> 
 <?php $year_travels = wp_get_post_terms($p_ID, \'year_travel\', array("fields" => "all")); ?>
 <?php $led_bys = wp_get_post_terms($p_ID, \'led_by\', array("fields" => "all")); ?>

 <!-- fetching tour data -->
 <?php foreach($tours as $tour):  ?>
 <?php $array_tour[\'id\'] = $tour->term_id; ?>
 <?php $array_tour[\'name\'] = $tour->name; ?>
 <?php endforeach; ?>

 <!-- fetching month data -->
 <?php foreach($month_travels as $month_travel):  ?>
 <?php $array_month[\'id\'] = $month_travel->term_id; ?>
 <?php $array_month[\'name\'] = $month_travel->name; ?>
 <?php endforeach; ?>

 <!-- fetching year data -->
 <?php foreach($year_travels as $year_travel):  ?>
 <?php $array_year[\'id\'] = $year_travel->term_id; ?>
 <?php $array_year[\'name\'] = $year_travel->name; ?>
 <?php endforeach; ?>

 <!-- fetching led by data -->
 <?php foreach($led_bys as $led_by):  ?>
 <?php $array_led_by[\'id\'] = $led_by->term_id; ?>
 <?php $array_led_by[\'name\'] = $led_by->name; ?>
 <?php endforeach; ?>

 <?php $categories_tour[] = array(\'id\' => $array_tour[\'id\'], \'name\' => $array_tour[\'name\']); ?> 
 <?php $categories_month[] = array(\'id\' => $array_month[\'id\'], \'name\' => $array_month[\'name\']); ?> 
 <?php $categories_year[] = array(\'id\' => $array_year[\'id\'], \'name\' => $array_year[\'name\']); ?> 
 <?php $categories_led_by[] = array(\'id\' => $array_led_by[\'id\'], \'name\' => $array_led_by[\'name\']); ?> 

 <?php endforeach; ?>

 <!--Removing duplicates-->
 <?php $input_tour = array_map("unserialize", array_unique(array_map("serialize", $categories_tour))); ?>
 <?php $input_month = array_map("unserialize", array_unique(array_map("serialize", $categories_month))); ?>
 <?php $input_year = array_map("unserialize", array_unique(array_map("serialize", $categories_year))); ?>
 <?php $input_led_by = array_map("unserialize", array_unique(array_map("serialize", $categories_led_by))); ?>

 <select id="type">
 <?php foreach($input_tour as $tour_category): ?>
 <?php echo "<option id=" . $tour_category[\'id\'] .">" . $tour_category[\'name\'] . "</option>"; ?>
 <?php endforeach; ?>
 </select>

 <select id="month">
 <?php foreach($input_month as $month_category): ?>
 <?php echo "<option id=" . $month_category[\'id\'] .">" . $month_category[\'name\'] . "</option>"; ?>
 <?php endforeach; ?>
 </select>

 <select id="year">
 <?php foreach($input_year as $year_category): ?>
 <?php echo "<option id=" . $year_category[\'id\'] .">" . $year_category[\'name\'] . "</option>"; ?>
 <?php endforeach; ?>
 </select>

 <select id="led_by">
 <?php foreach($input_led_by as $led_by_category): ?>
 <?php echo "<option id=" . $led_by_category[\'id\'] .">" . $led_by_category[\'name\'] . "</option>"; ?>
 <?php endforeach; ?>
 </select>
如果我在任何地方出错,请建议我。。。,谢谢

结束