Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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 or false 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 a WP_Error object. Calling empty($terms) will account for the first two, and is_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:

$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));   

if (!empty($parent_terms) && !is_wp_error($parent_terms)) {
    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));
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';  
            }
        }
    }
    echo '</ul>';
}

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:

wp_list_categories(array(
    'title_li' => null, // We don't need the automatically generated heading
    'depth' => 1, // Judging from your code you only want to print one level of terms
    'orderby' => 'slug',
    'taxonomy' => 'hhie_artists',
));

The result should follow this structure:

  • Parent artist A
    • Child artist A1
    • Child artist A2
    • Child artist A3
  • Parent artist B
    • Child artist B1
    • Child artist B2
share|improve this answer
    
Hi Simon, appreciate your answer. I had previously tried wp_list_categories but I'm having a few issues. The code above was originally intended to display "All Albums" on an Archives page. Albums are essentially children of my custom taxonomy "Artists". On the same page, I used a separate call to wp_list_categories to call only the parents to create an "All Artists" list. –  Joe Feb 5 '13 at 21:22
    
Sorry, ran out of time editing: I should have explained that I am using the code an an "All Archives" page, and I wanted to call a list of all the parent terms in my custom taxonomy "Artists", to display "All Artists". I then want to display "All Albums", where Albums are the children of custom taxonomy "Artists". I realise this defers from my original question, but that was what the code was intended to do until I ran into wp_error. Can you help? I can't get wp_list_categoriesto work correctly with my custom taxonomy, showing only parent terms in a list, and then another of child terms. –  Joe Feb 5 '13 at 21:34
    
I still don't see how the wp_list_categories example I posted above (I modified it to include your custom taxonomy) would generate anything other than what you are trying to achieve - have a look at the structural example output. But if it still does not work, checking the return value for WP_Error objects as in the first example should help you work around any errors. And if the issue is understanding why you get an error - try doing if (is_wp_error($terms)) var_dump($terms) or something. It should contain an explanation of the error, that's basically what its for. –  Simon Feb 5 '13 at 22:14
    
I've now succeeded using your provided code. I'm not sure what went wrong when I tried earlier. Thanks a lot. –  Joe Feb 5 '13 at 22:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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