0

Hi this is the code I am using on website, but it's giving me

Catchable fatal error: Object of class WP_Error could not be converted to string in /home/capritra/public_html/wp-content/themes/betheme/index.php on line 181

I have copied code from internet, it's working for people but not for me. Can someone help out?

 <?php
  $term_id = get_queried_object()->term_id;
  $taxonomy_name = get_query_var( 'cat' ); 
  $termchildren = get_term_children( $term_id, $taxonomy_name );

  // Loop through each child taxonomy term
  foreach ( $termchildren as $child ) {
      // Get the term object
      $term = get_term_by( 'id', $child, $taxonomy_name );

      // Get the values we need to output
      $url = get_term_link( $child, $taxonomy_name );
      $name = $term->name;

      // We make the call here to get the image and we pass in the term we want the image for
      $image = get_field('kategorija_slika', $term );

      // Output it all
      echo '<div class="col-sm-4 sub-caregory">';
      echo '<a href="' . $url . '">' . $name . '</a><div class="sub-caregory-image"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div>';
      echo '</div>';
  }
?> 
  • Which line in the code is 181 exactly? – Rarst Dec 10 '16 at 12:59
  • get_queried_object() might return a WP_Error instance. You have to test that (is_wp_error( $term )) before you use the properties. – fuxia Dec 10 '16 at 14:01
  • This is line 181 - echo '<a href="' . $url . '">' . $name . '</a><div class="sub-caregory-image"><img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '" /></div>'; – David Dec 10 '16 at 16:00
  • Where should I add is_wp_error? I'm newbie at PHP, and I tried copying solution that I found online. Thanks a bunch! – David Dec 10 '16 at 16:02
  • I have ditched the solution because it didn't workout as I needed it to. I have added is_wp_error, and it worked, but not as I need it to. – David Dec 10 '16 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.