Errore: L'oggetto della classe WP_Error non può essere convertito in stringa

18 apr 2014, 21:50:15
Visualizzazioni: 16.2K
Voti: 1

Dopo aver eliminato alcune categorie (cosa che non avrei dovuto fare), ricevo il seguente errore:

Catchable fatal error: Object of class WP_Error could not be converted to string in [FILENAME] on line 45

Ecco il file che ha causato l'errore. Questo è un estratto (la riga 45 è $output .= '<a href="' . $link . '">' . $term->name. '</a>, ';)

   ...
  foreach($item['value'] as $value) {
        $term = get_term($value, $fieldSettings['taxonomy']);
        $link = get_term_link($term);

        // nessuna opzione deve interrompere l'esecuzione!
        if(!$term) {
          continue;
        }

        if(isset($formatterSettings['link_bool']) && $formatterSettings['link_bool']) {
          $output .= '<a href="' . $link  . '">' . $term->name. '</a>, ';
        } else {
          $output .= $term->name . ', ';
        }
      }
    }
 ...

Come posso risolvere questo problema? Ho ricreato tutte le categorie eliminate (ho fatto il backup dei loro nomi e slug), ma l'errore persiste.

Grazie per il tuo aiuto!

Ecco il file completo:

namespace Hydra\Formatters;

use Hydra\Builder;

class TaxonomyFormatter extends BasicFormatter {

  public function __construct() {
    $this->name = 'taxonomy';
  }

  public function render(\HydraFieldViewRecord $viewView, $post) {

    $items = $this->getValues($viewView);
    if(!$items) {
      return $items;
    }

    $fieldSettings = $viewView->field->attributes;
    $formatterSettings = $viewView->settings;

    $meta = $viewView->field->meta;
    $output = '';


    foreach ($items as $item) {
      if(is_string($item['value'])) {
        if($item['value'] == 0) {
          continue;
        }
        $item['value'] = array($item['value']);
      }

      foreach($item['value'] as $value) {
        $term = get_term($value, $fieldSettings['taxonomy']);
        $link = get_term_link($term);

        // nessuna opzione deve interrompere l'esecuzione!
        if(!$term) {
          continue;
        }

        if(isset($formatterSettings['link_bool']) && $formatterSettings['link_bool']) {
          $output .= '<a href="' . $link  . '">' . $term->name. '</a>, ';
        } else {
          $output .= $term->name . ', ';
        }
      }
    }

    $output = trim($output, ', ');
    $terms = $output;

    $output = '';
    $output .= '<div ' . $this->printAttributes($viewView) . '>';

    if ($meta->prefix) {
      $output .= "<div class=\"field-prefix\" >" . $meta->prefix . "</div>";
    }

    $output .= "<div class=field-value>" . $terms . "</div>";
    if ($meta->suffix) {
      $output .= "<div class=\"field-suffix\">" . $meta->suffix . "</div>";
    }
    $output .= "</div>";

    return $output;
  }

  public function getSettingsForm($parentElement) {
    parent::getSettingsForm($parentElement);
    $parentElement->addField('checkbox', array('link_bool', __('Link to taxonomy page', 'hydraforms')))
      ->setDefaultValue(false);
  }
}
0
Tutte le risposte alla domanda 1
0

get_term può restituire un oggetto WP_Error oltre a un valore falsy quando il termine non è trovato o una riga effettiva del termine.

Puoi risolvere questo problema aggiungendo un controllo aggiuntivo:

if (!$term) {
   continue;
}

Diventa:

if (!$term || is_wp_error($term)) {
    continue;
}

Dovresti fare lo stesso anche prima della chiamata a get_term_link.

$term = get_term($value, $fieldSettings['taxonomy']);
if (!$term || is_wp_error($term)) {
    continue;
}

$link = get_term_link($term);

get_term solitamente restituisce un WP_Error quando la tassonomia non esiste o non è registrata (puoi consultare il codice sorgente per maggiori informazioni). Quindi assicurati che sia registrata. Se la stai registrando, assicurati che il codice sopra (che sta causando l'errore) venga eseguito dopo init, dove probabilmente la tassonomia viene registrata.

18 apr 2014 21:54:23