最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

notifications - Notice: Trying to get property of non-object in options.php

programmeradmin1浏览0评论

I am generating the following notice in my options.php when testing my theme in wp_debug mode.

I can see where the problem is but do not know how to fix this issue?

It seems the non object is being called from the taxonomy array in options.php as the array cannot find the term_id because a post and or category has not been created in the custom post type. When I create a post and assign a category to it, the notice disappears.

// Pull all the custom taxonomies into an array
$options_password_taxonomies = array();
$taxonomies_password_terms_obj = get_terms('password_gallery_category');
foreach ( $taxonomies_password_terms_obj as $taxonomy) {
    $options_password_taxonomies[$taxonomy->term_id] = $taxonomy->name;
}

// Select a Category for your Client Area
$options[] = array(
    'name' => __('Password Protected Galleries', 'shutter'),
    'desc' => __('Choose a category for password protected client galleries.',     'shutter'),
    'id' => 'client_area',
    'type' => 'select',
    'options' => $options_password_taxonomies);

I am generating the following notice in my options.php when testing my theme in wp_debug mode.

I can see where the problem is but do not know how to fix this issue?

It seems the non object is being called from the taxonomy array in options.php as the array cannot find the term_id because a post and or category has not been created in the custom post type. When I create a post and assign a category to it, the notice disappears.

// Pull all the custom taxonomies into an array
$options_password_taxonomies = array();
$taxonomies_password_terms_obj = get_terms('password_gallery_category');
foreach ( $taxonomies_password_terms_obj as $taxonomy) {
    $options_password_taxonomies[$taxonomy->term_id] = $taxonomy->name;
}

// Select a Category for your Client Area
$options[] = array(
    'name' => __('Password Protected Galleries', 'shutter'),
    'desc' => __('Choose a category for password protected client galleries.',     'shutter'),
    'id' => 'client_area',
    'type' => 'select',
    'options' => $options_password_taxonomies);
Share Improve this question asked Feb 27, 2013 at 10:04 Dean McCannDean McCann 111 gold badge1 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can use isset() or property_exists() to check if the property exists.

foreach ( $taxonomies_password_terms_obj as $taxonomy) {
    if( isset( $taxonomy->term_id ) ){
        $options_password_taxonomies[$taxonomy->term_id] = $taxonomy->name;
    }
}

useisset($variable_to_check) or use is_array($variable_to_check) before proceding foreach because foreach only works with an array of data ie your variable need members

发布评论

评论列表(0)

  1. 暂无评论