WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List terms and descriptions of a custom taxonomy (3 posts)

  1. jgtjones
    Member
    Posted 1 year ago #

    This should have been covered already but I cannot find a solution by search. Plenty of results, but they all disappear down blind alleys without answering my question.
    I want to list the terms available in a custom taxonomy with their descriptions. The code I have is

    $taxonomy = "whatever";
          $terms =get_terms($taxonomy);
          $count=count($terms);
          if ( $count > 0 ) {
            echo "<ul>";
            foreach ($terms as $term) {
              echo "<li>" . $term->name . ": " . $term->description . "</li>";
            }
            echo "</ul>";
          }

    This does not produce a complete list. I do not know what determines which terms to include, possible only those in published posts.
    What more do I need to do?

  2. jgtjones
    Member
    Posted 1 year ago #

    Ok. Solved by adding an obscure argument.
    Required code is:

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button before and after your code on a new line. ]

    $categories = get_terms( 'category', array(
     	'orderby'    => 'count',
     	'hide_empty' => 0
     ) );
  3. jgtjones
    Member
    Posted 1 year ago #

    Apologies for not being consistent and including code from elsewhere. The following php code will list all terms and descriptions from all public and custom (not built in, i.e. excluding categories and tags) taxonomies:

    $args=array(
      'public'   => true,
      '_builtin' => false
    
      );
      $output = 'names'; // or objects
      $operator = 'and'; // 'and' or 'or'
      $taxonomies=get_taxonomies($args,$output,$operator);
      if  ($taxonomies) {
        foreach ($taxonomies  as $taxonomy ) {
          echo '<p>'. $taxonomy. '</p>';
          $terms =get_terms($taxonomy, array('hide_empty' => 0));
          $count=count($terms);
          if ( $count > 0 ) {
            echo "<ul>";
            foreach ($terms as $term) {
              echo "<li>" . $term->name . ": " . $term->description . "</li>";
            }
            echo "</ul>";
          }
        }
      }

Topic Closed

This topic has been closed to new replies.

About this Topic