【问题标题】:Display Wordpress parent Category with their child category显示 Wordpress 父类别及其子类别
【发布时间】:2010-06-11 09:06:52
【问题描述】:

我只想显示那些有一些子类别的父类别及其子类别而不使用child_of=

我试图显示,但我只能获取子类别列表而不是其父类别名称。

<?php

$querystr = "SELECT wp_terms.name, wp_terms.term_id, wp_terms.name FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.parent !=0 ";
$cat_child = $wpdb->get_results($querystr, OBJECT);
var_dump($cat_child);
foreach ($cat_child as $category) {
         echo $category->name. ' , ';
      }
  ?>

帮帮我.. 谢谢

【问题讨论】:

  • 所以你想只显示给定类别的parent
  • 是的,我想显示该子类别的父类别

标签: wordpress


【解决方案1】:

这样就搞定了

<?php

                            $querystr = "SELECT wp_terms.name, wp_terms.term_id, wp_terms.name FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.parent !=0 ";
                            $cat_child = $wpdb->get_results($querystr, OBJECT);
                            var_dump($cat_child);
                            echo '<ul>';
                            foreach ($cat_child as $category) {
                                 $cat_id = intval($category->term_id);
                                 echo '<li>';
                                    echo get_category_parents($cat_id , TRUE , ' <br/> ');
                                 echo '</li>';
                            }
                            echo '</ul>';
                     ?>

谢谢

【讨论】:

    【解决方案2】:

    如果您不想使用“child_of”参数,那么您的问题可以通过使用两个循环来解决,一个用于displaying parent categories,另一个用于displaying its direct child categories.

      // get_categories() function will return all the categories
            $upaae_categories = get_categories( array(
             'orderby' => 'name',
             'order' => 'ASC'
            ) );
    
            foreach( $upaae_categories as $single_cat ) {
             if($single_cat->parent < 1) // Display if parent category and exclude child categories
             {
        echo 'Parent: '.$single_cat->name;
        // now get all the child categories
        $child_categories=get_categories(
            array( 'parent' => $single_cat->term_id )
        );
        if(sizeof($child_categories)>0){ /* this is just for ensuring that this parent category do have child categories otherwise a category cannot be a parent if does not have any child categories*/
        echo '###childs###</br>'
        foreach ($child_categories as $child) {
    
            echo $child->name.'</br>';
        }// end of loop displaying child categories
        } //end of if parent have child categories
    
             }
            } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多