Bird
0
0

Given this code snippet:

medium📝 component behavior Q13 of 15
Wordpress - Custom Post Types and Taxonomies
Given this code snippet:
register_taxonomy('genre', 'post', array('hierarchical' => true));
$terms = get_terms(array('taxonomy' => 'genre', 'hide_empty' => false));
foreach ($terms as $term) {
echo $term->name . ', ';
}

What will this code output if there are two terms 'Fiction' and 'Non-Fiction' assigned to posts?
Agenre, Fiction, Non-Fiction
BFiction, Non-Fiction,
CArray
DError: Undefined variable
Step-by-Step Solution
Solution:
  1. Step 1: Understand get_terms() usage

    get_terms() fetches terms of the taxonomy 'genre' including empty ones due to 'hide_empty' => false.
  2. Step 2: Loop through terms and print their names

    The foreach loop prints each term's name followed by a comma and space.
  3. Final Answer:

    Fiction, Non-Fiction, -> Option B
  4. Quick Check:

    Loop prints term names separated by commas [OK]
Quick Trick: get_terms returns term objects; loop prints their names [OK]
Common Mistakes:
  • Expecting taxonomy name instead of term names
  • Confusing array output with string
  • Not setting 'hide_empty' causing missing terms

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes