ordre hiérarchique get_categories comme wp_list_categories - avec nom, slug et lien pour modifier le chat
4 réponses
- votes
-
- 2012-02-07
sortie sousforme de listenon ordonnée:
<?php hierarchical_category_tree( 0 ); // the function call; 0 for all categories; or cat ID function hierarchical_category_tree( $cat ) { // wpse-41548 // alchymyth // a hierarchical list of all categories // $next = get_categories('hide_empty=false&orderby=name&order=ASC&parent=' . $cat); if( $next ) : foreach( $next as $cat ) : echo '<ul><li><strong>' . $cat->name . '</strong>'; echo ' / <a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>View ( '. $cat->count . ' posts )</a> '; echo ' / <a href="'. get_admin_url().'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$cat->term_id.'&post_type=post" title="Edit Category">Edit</a>'; hierarchical_category_tree( $cat->term_id ); endforeach; endif; echo '</li></ul>'; echo "\n"; } ?>
output as unordered list:
<?php hierarchical_category_tree( 0 ); // the function call; 0 for all categories; or cat ID function hierarchical_category_tree( $cat ) { // wpse-41548 // alchymyth // a hierarchical list of all categories // $next = get_categories('hide_empty=false&orderby=name&order=ASC&parent=' . $cat); if( $next ) : foreach( $next as $cat ) : echo '<ul><li><strong>' . $cat->name . '</strong>'; echo ' / <a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>View ( '. $cat->count . ' posts )</a> '; echo ' / <a href="'. get_admin_url().'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$cat->term_id.'&post_type=post" title="Edit Category">Edit</a>'; hierarchical_category_tree( $cat->term_id ); endforeach; endif; echo '</li></ul>'; echo "\n"; } ?>
-
génie!- hide_empty semblenécessiter une valeurnumériquepourfonctionner,le resteestparfait -merci!genius! - hide_empty seems to require a numeric value to work, the rest is spot on - thanks!
- 0
- 2012-02-08
- Q Studio
-
peut-être sans rapport avecma questioninitiale,mais commentpourrais-je déterminer le «niveau» de la hiérarchie des catégories sur laquelle setrouve chaque catégorie -parexemple dans une hiérarchie à 3niveaux,j'auraisbesoin d'obtenir 1,2 ou 3?perhaps unrelated to my original question, but how could I determine the "level" of the category hierarchy each category is on - for example in a 3 level hierarchy, I'd need to get 1, 2 or 3?
- 0
- 2012-02-08
- Q Studio
-
http://pastebin.com/GbZFnytw - sortie deprofondeur ajoutéeen tant que classe css de labalise ulhttp://pastebin.com/GbZFnytw - depth output added as css class of ul tag
- 0
- 2012-02-08
- Michael
-
incroyable - y a-t-il unmoyen d'imbriquer les ul/li -pour qu'ils s'enroulenttous -je voudraisessayer de lesmettre dans unenavigation ouverte/fermée detype accordéon?amazing - is there any way to nest the ul / li's - so that they all wrap around - I'd like to try and get them into an accordion type open / close navigation?
- 0
- 2012-02-09
- Q Studio
-
parimbrication,je voulais direby nesting I meant
- 0
- 2012-02-10
- Q Studio
-
nouveau code http://pastebin.com/TTvYPKPH - corrigé avec du code ajoutépour la sortie html structurée -ignorer l'ancienpastebin http://pastebin.com/GbZFnytwnew code http://pastebin.com/TTvYPKPH - corrected with added code for structured html output - ignore the old pastebin http://pastebin.com/GbZFnytw
- 0
- 2012-02-10
- Michael
-
merciencore,pourrais-je vousenvoyer un lien vers unepage,j'ai une autre question,maisje ne veuxpas rendre l'URLpublique?thanks again, could I PM you a link to a page, I have another question, but don't want to make the URL public?
- 0
- 2012-02-10
- Q Studio
-
pas deproblème - utilisez simplement l'e-mail lié àmonnom d'utilisateur.no problem - just use the email linked with my username.
- 0
- 2012-02-10
- Michael
-
oùpuis-jetrouver cete-mail?where do I find this email?
- 0
- 2012-02-11
- Q Studio
-
- 2015-10-09
Une version légèrementmise àjour de la réponse de Michael pour utiliser la get_terms (pour que vouspuissiez obtenir destaxonomiespersonnalisées,dans ce cas,je voulais lataxonomie des catégories deproduits WooCommerce de
product_cat
) .echo hierarchical_term_tree(); function hierarchical_term_tree($category = 0) { $r = ''; $args = array( 'parent' => $category, ); $next = get_terms('product_cat', $args); if ($next) { $r .= '<ul>'; foreach ($next as $cat) { $r .= '<li><a href="' . get_term_link($cat->slug, $cat->taxonomy) . '" title="' . sprintf(__("View all products in %s"), $cat->name) . '" ' . '>' . $cat->name . ' (' . $cat->count . ')' . '</a>'; $r .= $cat->term_id !== 0 ? hierarchical_term_tree($cat->term_id) : null; } $r .= '</li>'; $r .= '</ul>'; } return $r; }
Simplifié unpeupour supprimer le lien d'édition,etc. Vouspouvez les ajouter sinécessaire.
A slightly updated version of Michael’s answer to use the more generic get_terms (so you can get custom taxonomies, in this case I wanted the WooCommerce product category taxonomy of
product_cat
).echo hierarchical_term_tree(); function hierarchical_term_tree($category = 0) { $r = ''; $args = array( 'parent' => $category, ); $next = get_terms('product_cat', $args); if ($next) { $r .= '<ul>'; foreach ($next as $cat) { $r .= '<li><a href="' . get_term_link($cat->slug, $cat->taxonomy) . '" title="' . sprintf(__("View all products in %s"), $cat->name) . '" ' . '>' . $cat->name . ' (' . $cat->count . ')' . '</a>'; $r .= $cat->term_id !== 0 ? hierarchical_term_tree($cat->term_id) : null; } $r .= '</li>'; $r .= '</ul>'; } return $r; }
Simplified a little to take out the edit link etc. You can add those as required.
-
Erreur:erreurfatale PHP capturable: l'objet de la classe WP_Errorn'apaspu être convertien chaîne.Sur cette ligne:foreach ($next as $ cat).Error : PHP Catchable fatal error: Object of class WP_Error could not be converted to string. On this line : foreach ($next as $cat) .
- 0
- 2018-12-14
- Mehdi
-
Cela a l'airbien.Commentpourrais-je lemodifierpour afficher leterme de départen haut de la liste?Actuellement,ilmontretous lestermesenfants,non?J'utilise unidentifiant determenon-0,c'est-à-dire.unidentifiant determe valide,pas seulementtous.This looks nice. How could I modify it to actually show the starting term at the top of the list? Currently, it shows all of the child terms, right? I'm using a non-0 term ID, ie. a valid term ID, not just all of them.
- 0
- 2019-02-02
- Robert Andrews
-
- 2019-06-15
Vouspouvez utiliser le code suivant:
$args = array( 'hide_empty' => 0, 'echo' => 1, 'taxonomy' => 'category', 'hierarchical' =>1, 'show_count' => 1, ); function add_class_wp_list_categories($wp_list_categories) { $pattern = '/<li class="/is'; $replacement = '<li class="first '; return preg_replace($pattern, $replacement, $wp_list_categories); } add_filter('wp_list_categories','add_class_wp_list_categories'); echo wp_list_categories( $args );
You can use following code:
$args = array( 'hide_empty' => 0, 'echo' => 1, 'taxonomy' => 'category', 'hierarchical' =>1, 'show_count' => 1, ); function add_class_wp_list_categories($wp_list_categories) { $pattern = '/<li class="/is'; $replacement = '<li class="first '; return preg_replace($pattern, $replacement, $wp_list_categories); } add_filter('wp_list_categories','add_class_wp_list_categories'); echo wp_list_categories( $args );
-
- 2012-02-07
Hmm,je pense que vous devezinclure
'hierarchical' => 1,
dans votre liste d'arguments.Vous avez également une virgule detrop à lafin de la liste des arguments.Après le dernier argument,vousn'avezpasbesoin de virgule :)Voici unexemple complet:
$args = array( 'orderby' => 'name', 'order' => 'ASC', 'hierarchical' => 1, 'hide_empty' => '0' );
Hmm I think you need to include
'hierarchical' => 1,
in your args list. Also you have one comma too much at the end of the args list. After the last argument you do not need a comma :)Here's a complete example:
$args = array( 'orderby' => 'name', 'order' => 'ASC', 'hierarchical' => 1, 'hide_empty' => '0' );
-
En savoirplus sur http://codex.wordpress.org/Function_Reference/get_categories :)Read more at http://codex.wordpress.org/Function_Reference/get_categories :)
- 0
- 2012-02-07
- Ole Henrik Skogstrøm
-
Merci -mais celane les ordonnepas hiérarchiquement - le codex déclare: - Quand c'est vrai,les résultatsincluront des sous-catégories qui sont vides,tant que ces sous-catégories ont des sous-catégories quine sontpas vides.La valeurpar défautesttrue.Valeurs valides:Thanks - but this does not order them hierarchically - the codex states: -- When true, the results will include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty. The default is true. Valid values:
- 0
- 2012-02-08
- Q Studio
J'aibesoin detrouver unmoyen de listertoutes les catégories - vides ounon - dans une liste hiérarchique - comme wp_list_categories -montrant également le slug,lenom du chatet un lien àmodifier dans l'administrateur.
Voici ce quej'aijusqu'àprésent:
Toutestbon,maispasbien ordonné -juste une liste alphabétique.