0

This has had me baffled for days so any help would be appreciated. I cannot get the function get_terms to return anything. I've tried the following code in the index.php, single.php and page.php, inside and outside the loop, of the default Twenty Thirty theme as well as my custom theme and no category terms are returned. I've created 10 test category terms and applied them to different test posts and still nothing. It's not working on the live site or the local development site. I've also tried flushing the rewrite rules by going to the Permalink Settings page and re-saving, just to see if that helped.

Researching and finding any similar problem online has the same answer: add the hide empty part. So I did, to no avail.

Any ideas why this function would not be working?

<?php get_terms("category") ?>

<?php get_terms("category", array("hide_empty" => 0)) ?>

2
  • Try to print count as ` $terms = get_terms("category", array("hide_empty" => 0)); $count = count($terms);` – Bindiya Patoliya Nov 2 '13 at 6:50
  • Thank you for the reply, Bindiya. However, that did not work either. Milo's answer did the trick, though. – codeview Nov 2 '13 at 17:13
3

get_terms just returns an array of terms, it doesn't generate output. You have to do something with that array to see the results-

$categories = get_terms( "category" );
echo "<ul>";
foreach ( $categories as $category ) {
    echo "<li>" . $category->name . "</li>"; 
}
echo "</ul>";

See the other examples on the Codex page.

7
  • Ugh! Okay, thank you again, Milo. But this is confusing to me. I had tried the 1st 3 examples on that codex page & none of them work either (why are they examples if they need more code to work?). The 4th and 5th examples do work, now that I try them (I hadn't tried them before b/c after the 1st 3 didn't work I figured I was doing something wrong). But why does the get_terms function need this extra code to output the results, when the similar function (well, as a PHP novice, I would think is similar) wp_list_pages works very simply as <?php wp_list_pages(); ?> to output the results? – codeview Nov 2 '13 at 17:31
  • WordPress has two sets of functions for most tasks - one that outputs data, and one that returns data. There are an infinite number of ways you can output data, so having functions that let you control output are helpful. If you want an auto-generated list of terms without having to output them yourself, use the_terms instead. – Milo Nov 2 '13 at 17:49
  • As for why the Codex appears to have incomplete examples- it's often assumed that users have a certain level of php knowledge to be able to understand the examples. The Codex is a collaboratively created resource, much of it is added by regular users, so there are often consistency issues between entries and examples. But the good news is, anyone can fix that! I suggest spending a day or two with the php manual to get a solid background of the basics, variables, control structures, functions, etc.. – Milo Nov 2 '13 at 17:53
  • Ah, very helpful information about the 2 sets of functions. That makes more sense now. And I assumed the WP Codex was maintained by WP only. Knowing now that is not the case does explain some of the inconsistencies I've seen while reading different sections. – codeview Nov 2 '13 at 18:26
  • Oh, and I had tried the the_terms function but it only returns the terms, of a taxonomy, for the current post and I was needing all the terms in a specific taxonomy, listed on a page. – codeview Nov 2 '13 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.