“使用短代码小部件”,注意:尝试获取非对象的属性

时间:2016-01-30 15:54:39

标签: php wordpress

我收到了这一行的通知:$ team_options [$ category-> name] = $ category-> slug;

$team_categories = get_terms('team_types');
$team_options = array("All" => "");
foreach ($team_categories as $category) {
    $team_options[$category->name] = $category->slug;
}

1 个答案:

答案 0 :(得分:0)

您需要确保“team_type”分类标准有条款。

以下是几乎没有类型检查的代码:

$team_categories = get_terms('team_type');
if (!is_wp_error($team_categories)) {
    $team_options = array("All" => "");
    foreach ($team_categories as $category) {
        if (is_object($category) && isset($category->name) && isset($category->slug)) {
            $team_options[$category->name] = $category->slug;
        }
    }    
}

首先“if”(is_wp_error())检查get_terms函数是否返回错误(当没有“team_types”分类法的条款时)。

第二个“if”(is_object())检查$ category是否为对象,以及是否具有“name”和“slug”属性。这应该足以摆脱通知。