从子分类术语ID查询帖子

时间:2012-05-17 作者:Solomon Henry

这是我的结构。。。。

状态1

CT1-1
     - Car_Model Post-CT1-1 (the posts of child)
     - Car_Model Post-CT1-2
CT2-1
     - Car_Model Post-CT2-2
CT3-1
     - Car_Model Post-CT3-1
州2

CT1-2
CT2-2
CT3-2
状态3

CT1-3
CT2-3
CT3-3
以下是我所做的显示两个分类法和一个子分类法的三重下拉列表的逻辑

州(分类学)

  • 第1站------>州(分类学)
  • 第2站------>市(州分类学之子)
  • 第3站------>制造商(分类学)
    • 现在,在选择第2站时,我必须从分类学术语id查询帖子,而不是第3站中的制造商

       <!-- WORKING LOGIC Triple Drop Down Start -->
      
       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" ></script>
       <script type="text/javascript">
      $(function()
      {
          $(\'#main_cat\').change(function()
          {
              var $mainCat=$(\'#main_cat\').val();
      
              // call ajax
              $("#sub_cat").empty();
              $("#manu_cat").empty();
              $.ajax
              (
                  {
                      url:"<?php bloginfo(\'wpurl\'); ?>/wp-admin/admin-ajax.php",     
                      type:\'POST\',
                      data:\'action=my_special_ajax_call&main_catid=\' + $mainCat,
      
                      success:function(results)
                      {
                          //  alert(results);
                          $("#sub_cat").removeAttr("disabled");       
                          $("#sub_cat").append(results);
                      // it should be closed here an perform new ajax call not in success ok ? 
                          // Passing the sub_cat to the function for displaying 3rd drop down list
                          $(function()
                          {
                              $(\'#sub_cat\').change(function()
                              {
                                  var $subCat=$(\'#sub_cat\').val();
      
                                  // call ajax
                                  $("#manu_cat").empty();
                                  console.log($("#manu_cat"));
                                  $.ajax
                                  (
                                      {
                                          url:"<?php bloginfo(\'wpurl\'); ?>/wp-admin/admin-ajax.php",     
                                          type:\'POST\',
                                          data:\'action=my_special_ajax_call&main_brand_id=\' + $subCat,
      
                                          success:function(results)
                                          {
                                              //  alert(results);
                                              $("#manu_cat").empty();
                                              $("#manu_cat").removeAttr("disabled");      
                                              $("#manu_cat").append(results); 
                                          }
                                      }
                                  );                                    
                              });
                          }); // function end for manufature jQuery
                      }
                  }
              );                                    
          });
      });     
      // for manufacturer logic paste here
      
       </script>
       <style type="text/css">
       #content{width:auto; height:400px; margin:50px;}
       </style>
      
       <form role="search" method="get" id="searchform" action="<?php echo get_option(\'siteurl\');?>/">
      <div id="content">
          <?php 
              wp_dropdown_categories(\'show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Select State&name=main_cat&taxonomy=state\');
          ?>
          <select name="sub_cat" id="sub_cat" disabled="disabled"></select>
          <select name="manu_cat" id="manu_cat" disabled="disabled"></select>
      
          <input type="submit" id="searchsubmit" value="Search" />        
      </div>
      </form> 
      
      
      <!-- WORKING LOGIC Triple Drop Down End -->
      

      Functions.php

      function implement_ajax() {
      if(isset($_POST[\'main_catid\']))
              {
              $categories=  get_categories(\'child_of=\'.$_POST[\'main_catid\'].\'&hide_empty=0\'.\'&taxonomy=state\');
      //change the taxonomy state in your case
                foreach ($categories as $cat) {
                  $option .= \'<option value="\'.$cat->term_id.\'">\';
                  $option .= $cat->cat_name;
                  //$option .= \' (\'.$cat->category_count.\')\';
                  $option .= \'</option>\';
                }
      
                echo \'<option value="-1" selected="selected">Select City</option>\'.$option;
              die();
              } // end if
      
      if(isset($_POST[\'main_brand_id\']))
              {
              $categories=  get_categories(\'&hide_empty=0\'.\'&taxonomy=manufacturer\');
      //change the taxonomy manufacturer in your case
                foreach ($categories as $cat) {
                  $options .= \'<option value="\'.$cat->term_id.\'">\';
                  $options .= $cat->cat_name;
                  //$option .= \' (\'.$cat->category_count.\')\';
                  $options .= \'</option>\';
                }
      
                echo \'<option value="-1" selected="selected">Select Make</option>\'.$options;
              die();
              } // end if
      
      
      }
      add_action(\'wp_ajax_my_special_ajax_call\', \'implement_ajax\');
      add_action(\'wp_ajax_nopriv_my_special_ajax_call\', \'implement_ajax\');//for users that are not logged in.
      
      从这一行开始的逻辑if(isset($_POST[\'main_brand_id\'])) 必须更换,所以这里if(isset($_POST[\'main_brand_id\'])) 我必须通过分类法的term\\u id来检索帖子,请帮助我查询逻辑以检索相关帖子标题。希望有人能帮助我。。。。。。。。

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

    我解决了这个问题

    if(isset($_POST[\'main_brand_id\']))
            {
            $term_id .= $_POST[\'main_brand_id\'];
            $term = get_term( $term_id, \'state\' );
            $slug = $term->slug;
            global $post;
            $post_id = $post->ID;
            $args = array( \'post_type\' =>\'dealers\',\'taxonomy\'=>\'state\', \'term\' => $slug );
            $myposts = get_posts( $args );
    
            foreach( $myposts as $post ) :    setup_postdata($post);
                echo \'<option value="\'.$post->ID.\'">\'
                    $options .=  the_title();
                echo \'</option>\';
            endforeach;
            die();
            } // end if
    
    上面的代码在选择子分类术语时在下拉列表中显示帖子标题。请继续使用代码。。。。。。。。。。。

    结束

    相关推荐

    get_the_terms issue

    我试图列出附加到相关帖子的自定义分类术语(这是一种自定义帖子类型)。我想把它列出来,而不是一个链接,它实际上是一个标题。我试过了,但显然不行。我做错了什么?function woocommerce_output_product_brand() { $terms = get_the_terms( $post->ID, \'brand\' ); foreach($terms as $term){ echo $term; } } 提前感谢!