【问题标题】:WP_Error in a shortcode to show terms简码中的 WP_Error 以显示条款
【发布时间】:2020-09-24 16:41:45
【问题描述】:

我正在使用 Anywhere Elementor 来设计分类归档模板。我使用这个简码来显示所有术语

// Add Shortcode
function categorias_shortcode() {
    $taxonomyName = "families";
    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false, 'suppress_filters' => false));   
    echo '<ul style="list-style: none; margin: 0; color: white !important;">';
    foreach ($parent_terms as $pterm) {
        $terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
        echo '<li><a style="color: white !important;" href="' . get_term_link( $pterm->name, $taxonomyName ) . '">' . $pterm->name . '</a></li>';
        foreach ($terms as $term) {
            echo '<li style="margin-left: 20px;"><a style="color: white !important;" href="' . get_term_link( $term->term_id, $taxonomyName ) . '">' . $term->name . '</a></li>';  
        }
    }
    echo '</ul>';
}
add_shortcode( 'categorias', 'categorias_shortcode' );

在我尝试翻译此页面之前,它运行良好。 WordPress 给我这个错误:

 Recoverable fatal error: Object of class WP_Error could not be converted to string in /usr/home/lloretensejove.cat/web/wp-content/themes/generatepress-child/functions.php on line 129 

这是第 129 行:

echo '<li><a style="color: white !important;" href="' . get_term_link( $pterm->name, $taxonomyName ) . '">' . $pterm->name . '</a></li>';

我不知道如何解决这个错误。

【问题讨论】:

  • 如果您缺少分类法的 wpml 翻译,那么我猜他根本找不到分类法并且失败了。你应该检查你的翻译,然后看看 $parent_terms 包含什么
  • 谢谢@Diego!它现在正在工作!

标签: wordpress elementor wpml


【解决方案1】:

我解决了这个错误,只是将这个 $pterm->name 改为这个 $pterm->term_id 就行了:

echo '<li><a style="color: white !important;" href="' . get_term_link( $pterm->term_id, $taxonomyName ) . '">' . $pterm->name . '</a></li>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多