Question

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);
Was it helpful?

Solution

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;
    }
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top