wordpress, get category names for a custom post type

前端 未结 3 372
-上瘾入骨i
-上瘾入骨i 2021-01-15 03:20

Is there a better way to get the category names for a custom post type in wordpress?



        
3条回答
  •  我在风中等你
    2021-01-15 03:59

    Assuming your custom taxonomy is recipegroups, i have implemented and tested this code in functions.php and i am sure that it will work in plugins too.

    $recipeTerms = get_terms(array( 
        'taxonomy' => 'recipegroups',
    
    ));
            foreach($recipeTerms as $recipeTerm){
                if($recipeTerm->parent==0){
                    echo "
    "; // remove these div's to suit your needs.. $termLink =get_term_link( $recipeTerm ); echo "
    ".$recipeTerm->name."
    "; $termChilds = get_term_children($recipeTerm->term_id, 'recipegroups' ); foreach($termChilds as $child){ $chTerm = get_term_by( 'id', $child, 'recipegroups'); $termLink =get_term_link( $chTerm ); echo "
    ".$chTerm->name."
    "; } echo "
    "; // end of termsBox div } }

提交回复
热议问题