最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Show all terms of a custom taxonomy?

programmeradmin4浏览0评论

I made a few custom taxonomies and I need to show ALL the terms from it, what I achieved so far is showing the taxonomies that are selected/chosen in a custom post type but I need all of them to show, wether it's selected or not. So that later I can make a filter that filters according to which terms a custom post type value contains.

<?php 
$args=array(
  'public'   => true,
  '_builtin' => false
);
$output = 'names'; // or objects
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator); 
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    $terms = get_terms($taxonomy);
        foreach ( $terms as $term) {
?>
        <li><br><a href="#"><input type="checkbox" name="profile[]" value=""><?php echo $term->name; ?> <br/></a></li>
       <?php 
                }
              }
            }  
            ?>

What I have so far.

Thanks in advance!

I made a few custom taxonomies and I need to show ALL the terms from it, what I achieved so far is showing the taxonomies that are selected/chosen in a custom post type but I need all of them to show, wether it's selected or not. So that later I can make a filter that filters according to which terms a custom post type value contains.

<?php 
$args=array(
  'public'   => true,
  '_builtin' => false
);
$output = 'names'; // or objects
$operator = 'and';
$taxonomies=get_taxonomies($args,$output,$operator); 
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    $terms = get_terms($taxonomy);
        foreach ( $terms as $term) {
?>
        <li><br><a href="#"><input type="checkbox" name="profile[]" value=""><?php echo $term->name; ?> <br/></a></li>
       <?php 
                }
              }
            }  
            ?>

What I have so far.

Thanks in advance!

Share Improve this question edited Mar 4, 2014 at 14:29 David H asked Mar 4, 2014 at 14:15 David HDavid H 7612 gold badges7 silver badges16 bronze badges 2
  • At what point does this fail? How much of it works the way you'd like? – s_ha_dum Commented Mar 4, 2014 at 14:26
  • It works the issue is that I can only show the SELECTED terms in a custom post type. I want all of them to show wether selected or not, I don't want to have a dummy post type that has everything selected just to show them. – David H Commented Mar 4, 2014 at 14:28
Add a comment  | 

4 Answers 4

Reset to default 89

You need to pass an additional argument to get_terms(). The default is to hide "empty" terms-- terms which are assigned to no posts.

$terms = get_terms([
    'taxonomy' => $taxonomy,
    'hide_empty' => false,
]);

EDIT:

Incase you want to display the name or the slug of the enlisted custom taxonomies being held by the $terms variable you can use this piece of code bellow:

foreach ($terms as $term){
  echo $term->slug." : ";
  echo $term->name;
  echo "<br><br>";
}

Where $term->slug outputs the slug of the taxonomy item that's enlisted and $term->name outputs the name of the according taxonomy item.

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array so:

$terms = get_terms( array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
) );

where terms that have no posts are hidden by default.

This code is fetches all category and subcategory custom taxonomies using get_terms():

<?php $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'parent' =>0)); 
   foreach($wcatTerms as $wcatTerm) : 
   ?>
<ul>
   <li>
      <a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a>
      <ul class="megaSubCat">
         <?php
            $wsubargs = array(
               'hierarchical' => 1,
               'show_option_none' => '',
               'hide_empty' => 0,
               'parent' => $wcatTerm->term_id,
               'taxonomy' => 'product_cat'
            );
            $wsubcats = get_categories($wsubargs);
            foreach ($wsubcats as $wsc):
            ?>
         <li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a></li>
         <?php
            endforeach;
            ?>  
      </ul>
   </li>
</ul>
<?php 
   endforeach; 
   ?>
<div class="col-sm-4">  
                <?php 
                $categories = get_terms( array(
                        'taxonomy' => 'freelancer_type',
                        'hide_empty' => false,
                        'parent' => 0,
                    ) );
                ?>
                <select class="form-control" name="parent_category" id="parent_category">
                    <option value="">Select Category</option>
                    <?php  
                        foreach($categories as $category) {
                            ?>
                                <option value="<?php echo $category->term_id; ?>"><?php echo $category->name; ?></option>
                            <?php 
                        }   
                            
                    ?>      
                </select>
                <select class="form-control" name="child_category" id="child_category">
                    <option value="">Select Sub Category</option>
                </select>
            </div>
发布评论

评论列表(0)

  1. 暂无评论