PHP可捕获致命错误:无法将类WP_ERROR的对象转换为字符串

时间:2017-06-12 作者:Sergi

我需要帮助:

PHP可捕获致命错误:WP\\u error类的对象无法转换为/hosting/www/ortopediairati中的字符串。es/公共/wp内容/主题/irati/rt框架/功能/rt\\U面包屑。php在线58

我已经更新到Wordpress 4.8,网络运行不好。

完整文件位于此处:

<?php

/* RT-Breadcrumb Function */
function rt_breadcrumb(){
global $taxonomy,$term_slug,$post,$delimiter;

if(!get_option(\'rttheme_breadcrumb_menus\')){
    return false;
}

//Markup 
$before=\'<div class="breadcrumb">\'.wpml_t(THEMESLUG, \'Breadcrumb Menu Text\', get_option(THEMESLUG.\'_breadcrumb_text\')).\' \';
$after=\'</div>\';
$delimiter=\' | \';

/*  WooCommerce Breadcrumb */ 
if ( function_exists( \'woocommerce_breadcrumb\' ) ) {
    if( is_woocommerce() ){
        $defaults = array(
            \'delimiter\'  => $delimiter,
            \'wrap_before\'  => $before,
            \'wrap_after\' => $after,
            \'before\'   => \'\',
            \'after\'   => \'\',
            \'home\'    => null
        );

        woocommerce_breadcrumb($defaults); 
        return false;
    }
} 


echo $before;

//Home Page
echo "<a href=\\"". BLOGURL ."\\" title=\\"". get_bloginfo(\'name\')."\\">".__( \'Home\', \'rt_theme\' )."</a>";

// page parents function
function page_parents($parent_page_id,$child_pages){
    global $delimiter;

    $parent_page = get_page($parent_page_id);
    $page_parents = $delimiter."<a href=\\"".get_permalink($parent_page->ID)."\\" title=\\"". get_the_title($parent_page->ID) ."\\" >". get_the_title($parent_page->ID) ."</a>" .$child_pages;

    if ($parent_page->post_parent) page_parents($parent_page->post_parent,$page_parents);

    else echo $page_parents;

}


// term parents function
function term_parents($term_id,$child_terms){
    global $taxonomy,$delimiter;

    $parent_term = get_term_by(\'ID\',$term_id, $taxonomy);
    $term_parents = $delimiter."<a href=\\"".get_term_link($parent_term->slug,$taxonomy)."\\" title=\\"". $parent_term->name ."\\" >". $parent_term->name ."</a>" .$child_terms;

    if ($parent_term->parent) term_parents($parent_term -> parent,$term_parents);

    else echo $term_parents;

}

//get start page
function get_start_page($start_page){
    global $delimiter;

    //start page parents
    $get_start_page=get_page($start_page);
    if ($get_start_page -> post_parent){
        page_parents( $get_start_page -> post_parent,\'\'); 
    }

    //start page
    if ($start_page && !get_query_var(\'lang\')) {
        echo  $delimiter."<a href=\\"".get_permalink($start_page)."\\" title=\\"". get_the_title($start_page) ."\\" >". get_the_title($start_page) ."</a>";
    }
}

//terms
function term_links(){
    global $taxonomy,$post_type,$term_slug,$delimiter;

    //Find start page and define taxonomy names
    if($taxonomy=="product_categories"){
        $start_page=get_option(\'rttheme_product_list\');
    }elseif($taxonomy=="portfolio_categories"){
        $start_page=get_option(\'rttheme_portf_page\');
    }   

    //get start page
    if ($start_page) get_start_page($start_page);


    $term=get_term_by(\'slug\',$term_slug, $taxonomy);

    //parent terms
    if (is_object($term) && $term -> parent){
        echo term_parents($term -> parent,\'\');   
    } 

    //current term
    if(is_object($term) && $term->slug) echo  $delimiter."<a href=\\"".get_term_link($term->slug,$taxonomy)."\\" title=\\"". $term->name ."\\" >". $term->name ."</a>";
}



//Pages
if ( is_page() ){
    //parent pages
    if ($post -> post_parent){
        page_parents( $post -> post_parent,\'\'); 
    } 

    //current page
    echo  $delimiter ."". $post->post_title;
}

//Single
elseif (is_single() && !is_attachment()){ 
    // Get post type
    $post_type = get_post_type();

    //Taxonomies
    if($post_type == \'products\' || $post_type == \'portfolio\'){

        term_links();
        //current page
        echo  $delimiter."<a href=\\"".get_permalink()."\\" title=\\"". get_the_title() ."\\" >". get_the_title() ."</a>";

    }else{
    //Categories

    //start page
    $start_page=get_option(\'rttheme_blog_page\');

    //get start page
    if ($start_page) get_start_page($start_page);

        $category_id = get_the_category();
        $category_id = $category_id[0]->cat_ID;//only one category can be show in the list  - the first one
        echo $delimiter;
        if($category_id){
            echo get_category_parents($category_id, TRUE, $delimiter, FALSE );
        }
        echo $post->post_title;
    }

//Category
}elseif (is_category()){
    //start page
    $start_page=get_option(\'rttheme_blog_page\');

    //get start page
    if ($start_page) get_start_page($start_page);

        echo $delimiter."".get_category_parents(get_query_var(\'cat\'), TRUE, $delimiter, FALSE);

//Taxonomy
}elseif (is_tax()){
    term_links();
} else {
    echo  $delimiter."";
    wp_title(\'\');
}

echo $after;

wp_reset_query(); 
}
?>
有人能帮帮我吗?

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

错误显示的第58行是这一行:

$parent_term = get_term_by(\'ID\',$term_id, $taxonomy);
$term_parents = $delimiter."<a href=\\"".get_term_link($parent_term->slug,$taxonomy)."\\" title=\\"". $parent_term->name ."\\" >". $parent_term->name ."</a>" .$child_terms;
我们的错误是:

类WP\\u Error的对象无法转换为字符串

这意味着被附加的那些东西之一不是"string", 但实际上WP_Error 对象

我会怀疑get_term_link, 这可能是返回错误对象的原因,这表明:

术语不存在分类法未注册或无效传递的值意外为空$parent_term 不是一个术语,而是一个错误对象,它将我们引向这里的课程:

Sometimes functions return error objects and you have to check for that, don\'t just assume it succeeded

如何检查错误让我们举一个总是失败的例子:

$value = get_term_link( "not a real term","fake taxonomy" );
if ( is_wp_error( $value ) ) {
    // something went wrong
}
is_wp_error 如果$value 是错误对象。某些函数可能返回falsenull, 所以!empty( $value ) 也是一个有用的检查

错误对象可以包含错误代码和消息,您可以使用get_error_message() 显示它们的方法:

$value = get_term_link( "not a real term","fake taxonomy" );
if ( is_wp_error( $value ) ) {
    // something went wrong
    echo $value->get_error_message();
}
您还可以设置$value 改为手动设置为默认值

关于globals的最后一点说明是,代码使用了全局变量,但这些变量具有超泛型名称,例如$taxonomy. 其他插件也可能使用这些插件,它们可能会发生冲突。函数名也是如此。

更好的前缀:

// term parents function
function sergi_term_parents( $term_id, $child_terms ) {
    global $sergi_taxonomy, $sergi_delimiter;
最好使用依赖注入并完全消除全局变量:

// term parents function
function sergi_term_parents( $term_id, $child_terms, $taxonomy, $delimeter ){
现在您的term_parents 函数永远不会冲突,适用于任何分类法或delimeter

结束