Vérifiez si la catégorie actuelle a des enfants
3 réponses
- votes
-
- 2013-03-28
Il y apeut-être unemeilleurefaçon de lefaire ounon,mais voici commentje procéderais:
$term = get_queried_object(); $children = get_terms( $term->taxonomy, array( 'parent' => $term->term_id, 'hide_empty' => false ) ); // print_r($children); // uncomment to examine for debugging if($children) { // get_terms will return false if tax does not exist or term wasn't found. // term has children }
Si leterme detaxonomie actuel a desenfants,lafonction
get_terms
renverra untableau,sinonelle renverrafalse
.Testéet fonctionne surmoninstallation vanilla locale avec leplugin Interface utilisateur detype demessagepersonnalisé utilisépourGénération CPT.
There may or may not be a better way to do this, but here's how I would do it:
$term = get_queried_object(); $children = get_terms( $term->taxonomy, array( 'parent' => $term->term_id, 'hide_empty' => false ) ); // print_r($children); // uncomment to examine for debugging if($children) { // get_terms will return false if tax does not exist or term wasn't found. // term has children }
If current taxonomy term has children the
get_terms
function will return an array, otherwise it will returnfalse
.Tested and works on my local vanilla install with Custom Post Type UI plugin used for CPT generation.
-
Quandj'ai décommenté leprint_r ($ children) ....il a sorti untableau.Comment celapourrait-il êtretransforméen si/autre?Désolé,je suisencoretrèsnoviceen phpWhen I uncommented the print_r($children).... it outputted an array. How could that be turned into an if/else? Sorry, I'm still very new to php
- 0
- 2013-03-28
- user29489
-
Ignorerj'ai utilisé ceci:if ($ children) { echo 'Lesenfantsici'; } autre { echo 'Pas d'enfants'; }Disregard I used this: if ($children) { echo 'Children Here'; } else { echo 'No Children'; }
- 1
- 2013-03-28
- user29489
-
@ user29489 Vous avez raison,je n'aipas été assez clair dansma réponse.Éditépour référencefuture.@user29489 You're right, I wasn't clear enough in my answer. Edited for future reference.
- 0
- 2013-03-28
- montrealist
-
Pour ceux qui ontjustebesoin de savoir s'il y a desenfantset quin'ontpasbesoin d'obtenir les donnéespour lestermesenfants,je suggère d'ajouter ``field '=>' count ''pour simplement compter lenombre d'enfants.For those that just need to know whether there are any children and don't need to get the data for the child terms, I suggest adding `'field' => 'count'` to just count the number of children.
- 1
- 2013-12-30
- J.D.
-
Celafonctionne-t-il également avec les catégories depublicationnormales?Does this work with normal post categories as well?
- 0
- 2018-11-19
- Pete
-
- 2013-12-12
Ilexiste également unepossibilité WPgénérique de lefaire via get_term_children .
<?php $children = get_term_children($termId, $taxonomyName); if( empty( $children ) ) { //do something here }
There's also a generic WP possibility to do this via get_term_children.
<?php $children = get_term_children($termId, $taxonomyName); if( empty( $children ) ) { //do something here }
-
- 2016-05-20
En supposant que vousessayez defiltrer vostermespour afficher uniquement lestermes qui ont desenfants ounon,vouspouvezen fait utiliser leparamètre
childless
dans votrefonctionget_terms()
.$children = get_terms( 'taxonomy' => '$taxonomy_slug', 'hide_empty' => false, 'childless' => true ) );
Cela affichera untableau determes quin'ontpas d'enfants.
Assuming that you are trying to filter your terms to only show terms that either have children or not, you can actually use the
childless
parameter in yourget_terms()
function.$children = get_terms( 'taxonomy' => '$taxonomy_slug', 'hide_empty' => false, 'childless' => true ) );
This will output an array of terms that don't have children.
Je doisindiquer si lapage d'archive detaxonomiepersonnalisée actuelle queje consulte comporte des catégoriesenfants. J'ai une situation oùil y abeaucoup de catégoriespersonnalisées avec desenfantset le sitene sert qu'à afficher lesmessagesen fin de ligne. Sinon,il devrait afficher un lien vers la catégorie qui constitue laprochaine étape. J'aitrouvé cetextrait de code,maisilne semblepasfonctionnerpour lestaxonomiespersonnalisées.