Custom Taxonomy Template Error: Catchable fatal error: Object of class WP_Error could not be converted to string

Been trying to chase down an error on this page:Catchable fatal error: Object of class WP_Error could not be convertedto

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".

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".

Share Improve this question edited Sep 12, 2015 at 8:07 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked May 7, 2014 at 19:17 Trish ThompsonTrish Thompson 111 gold badge1 silver badge3 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 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.

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.

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;

This worked for me


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

foreach ( $terms as $term ) {

echo $term->name;

}

?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745092737a4610790.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信