1

I get this error after deleted a few of unused category's product on my woo theme:

Catchable fatal error: Object of class WP_Error could not be converted to string in D:-mytheme\wordpress\wp-content\plugins\myplugin\mymodule.php on line 47

The line 47 is like this:

 <?php echo '<a href="'.get_term_link($term).'">'.$term->name.'</a>'; ?>

The $term define on:

 <?php 
 class myShortCode {
    protected function content($atts, $content = null){
    $atts =  vc_map_get_attributes( $this->getShortcode(), $atts );
     global $woocommerce, $product; 
    extract( shortcode_atts( array(
        'title' => ''       
    ), $atts ) );
    $term = get_term_by('name', $type_product, 'brands');
    $category = isset($term->term_id) ? $term->term_id : '';    
    $css_class  = "";

    if ( $css_animation != '' ) {
        wp_enqueue_script( 'waypoints' );
        $css_class = ' wpb_animate_when_almost_visible wpb_'.$css_animation;
}       
 ;
 ob_start();
 ?>

The error disappear only id the category exist and selected.

I have no idea how to fix this. Can anyone throw me a bone?

Thank you.

  • Before accessing $term->name check if it is not wp error object. – Sumit May 14 '16 at 11:19
1

You can wrap it in the same check used for $category:

<?php if (isset($term->term_id)) {
    echo '<a href="'.get_term_link($term).'">'.$term->name.'</a>';
} ?>

or check to make sure it is not an error object:

<?php if (!is_wp_error($term)) {
    echo '<a href="'.get_term_link($term).'">'.$term->name.'</a>';
} ?>

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.