I’ve seen other people with similar problems, and I know that I need to check the return when calling for the child items in my custom taxonomy “hhie_artists”. I’m not sure how to echo
my output to incorporate wp_error
.
Here is my code:
<?php
$taxonomyName = "hhie_artists";
//Call top layer terms only (by setting Parent to 0).
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => true));
echo '<ul>';
foreach ($parent_terms as $pterm) {
//Get the Child terms
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => true));
foreach ($terms as $term) {
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
}
echo '</ul>';
?>
From what I’ve found out, this is due to an empty taxonomy field being found. I’ve yet to populate all my custom taxonomy fields, so I’m not surprised by this error, I just need to solve it. Thanks.
Not entirely sure what you are asking, but if I understand correctly you want to perform some testing on the return value in order to avoid iterating a
WP_Error
object and perhaps handle errors in some way.According to the documentation
get_terms()
will return an array of term objects orfalse
if no objects were found, but clearly by looking at the source it will return a WP_Error object if the taxonomy does not exists or in some cases an empty array.Thus, to check for errors you will have to check if the return value matches
false
, empty array (array()
) or aWP_Error
object. Callingempty($terms)
will account for the first two, andis_wp_error($terms)
can be used for testing if the return value is an error object. Something like this will work for performing such testing:However, there is already a function available in WordPress for printing a category tree like this. It’s called
wp_list_categories
and i strongly suggest you use that instead. Here’s how:The result should follow this structure: