1

Been trying to chase down an error on this page:

Catchable fatal error: Object of class WP_Error could not be converted to string

I have a custom post type and taxonomy:

Taxonomy: team_categories

  • Term: "Committees" (among others)

    • Child terms under committees: Continuing Education, Ethics, Legislative Action, etc. (* this part seems to be working)

      • Team members in each committee (not working)

I want to list the child terms on the "Committees" taxonomy template page and display the members of each committee, for instance:

Continuing Education committee

  • Jane Doe
  • John Brown
  • etc.

Ethics committee

  • Jack Jones
  • Ann Acme
  • etc.

Here is the current code:

$taxonomyName = "team_categories";
//This gets top layer terms only.  This is done by setting parent to 0.  
$parent_terms = get_terms(
    $taxonomyName, 
    array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false )
);   
echo '<ul>';
foreach ( $parent_terms as $pterm ) {
    //Get the Child terms
    $terms = get_terms(
        $taxonomyName, 
        array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false )
    );
    foreach ( $terms as $term ) {
        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . 
            $term->name . '</a></li>';  
    }
}
echo '</ul>';

The error appears to be here:

        echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . 
            $term->name . '</a></li>'; 

Update:

The following code gets me darn close to what i'm looking for:

$term_id = 26; // id of committees
$taxonomy_name = 'team_categories';
$termchildren = get_term_children( $term_id, $taxonomy_name );                      
echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . 
        $term->name . '</a></li>';
}
echo '</ul>';

It lists all of the sub categories in the "Committees" category.

How can I display each category's posts under each category heading? The custom post type's name is "team".

1

Well, you echo an object. Never just echo stuff if you are not sure what you get in return. Take a look at the function: The error itself is quite clear:

function get_term_link( $term, $taxonomy = '') {
    global $wp_rewrite;

    if ( !is_object($term) ) {
        if ( is_int($term) ) {
            $term = get_term($term, $taxonomy);
        } else {
            $term = get_term_by('slug', $term, $taxonomy);
        }
    }

    if ( !is_object($term) )
        $term = new WP_Error('invalid_term', __('Empty Term'));

You seem to not be getting an object in return, so possibly your $term->name is wrong (or empty).

To test for an error use is_wp_error() and to output the message:

$link = get_term_link( etc );
if ( is_wp_error( $link ) )
    echo $link->get_error_message();

Then you should get proper output of what happened and should be able to fix it.

  • Thanks kaiser for your help. I added the "is_wp_error" code above to the page and removed the "echo" line. The page reported "Empty TermEmpty TermEmpty TermEmpty Term" I wasn't sure if the function you mentioned is something that exists in wp backend or if you meant for me to add that to the taxonomy page. – Trish Thompson May 8 '14 at 15:40
  • @TrishThompson You must check for errors. There is no way around it. Else it will fail in another case. When you get Empty Term then this is the error provided by WordPress - and that means that your terms array is ... empty. – kaiser May 8 '14 at 19:43
0

EDIT: I had suggested adding endforeach;, but I just relooked at the code example in the codex and they don't use it, so it doesn't look necessary.

Another suggestion would be to follow the example for get_term_link() without using the nested foreach() to see if you can get it to produce the desired "Top Layer Term" results.

One benefit of their code example is that seems to continue through an error:

$terms = get_terms( 'team_categories' );

echo '<ul>';

foreach ( $terms as $term ) {

    // Sanitize the term, since we will be displaying it.
    $term = sanitize_term( $term, 'team_categories' );

    $term_link = get_term_link( $term, 'team_categories' );

    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }

    // We successfully got a link. Print it out.
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
}

echo '</ul>';

If that works, then add the extra complexity using the example style of code formatting.

  • 1
    Thanks Stephen for your help as well. tested the code above and it produced the list of all team_categories. I tried changing "team_categories" to "committees" (which is the category that has the sub categories i want listed) but is produced an empty page. I'm quite new to wp code, so i wasn't sure how to add the extra complexity using your example style above - gonna give it a try though :). – Trish Thompson May 8 '14 at 15:49
  • try $terms = get_terms('committees'); var_dump($terms); to see if $terms is even being loaded with any data – Stephen S. May 8 '14 at 16:10
  • Thanks Stephen - i've added an update to the post. Found some code that helps. – Trish Thompson May 8 '14 at 19:00
0

What worked for me was fixing my syntax for referring to the part of the object within the array.

The correct code in my case was echo $web_desc_title[$i]->name;

0

This worked for me


$terms = get_the_terms( $post->ID , 'taxonomyname' );

foreach ( $terms as $term ) {

echo $term->name;

}

?>
  • This is a completely different situation, because your terms are relying on a post id. – fuxia Sep 26 at 12:40

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.