2010-06-11 5 views
2

Je veux afficher uniquement ceux de la catégorie parente qui ont une certaine catégorie d'enfants avec leur catégorie enfant sans utiliser child_of=Afficher parent Wordpress Catégorie de leur catégorie enfant

je cherchais à afficher, mais je ne suis en mesure d'obtenir la liste de la catégorie enfant pas le nom de la catégorie mère.

<?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. ' , '; 
     } 
    ?> 

Aidez-moi .. Merci

+0

Donc, vous voulez montrer seulement le * parent * d'une catégorie donnée? – TheDeadMedic

+0

oui je veux afficher la catégorie parente de cette catégorie enfant – Soarabh

Répondre

2

fait en faisant ce

<?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>'; 
        ?> 

Merci

0

Si vous ne voulez pas utiliser argument "child_of" alors votre problème peut être résolu en utilisant deux boucles une pour displaying parent categories et d'autres pour 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 

     } 
     } 
Questions connexes