WordPress: Cannot use object of type stdClass as array

I get this array error

"Fatal error: Cannot use object of type stdClass as array" 

in WordPress on file category.php. The issue arrives when I don’t have any subcategories of a category, so I try to handle if the subcategory list is 0.

Read More

But I keep getting the same error, can someone see the error I do here?

The error appears starting in the line:

"if ($category[0]->category_parent == 0) {"

And the whole code within the brackets.

<?php 
                                $args = array(
                                    'type' => 'post',
                                    'child_of' => $cat_id,
                                    'parent' => get_query_var(''),
                                    'orderby' => 'name',
                                    'order' => 'ASC',
                                    'hide_empty' => 0,
                                    'hierarchical' => 1,
                                    'exclude' => '',
                                    'include' => '',
                                    'number' => '',
                                    'taxonomy' => 'category',
                                    'pad_counts' => true );

                                $categories = get_categories($args);


                                        if ($category[0]->category_parent == 0) {

                                            $tag = $category[0]->cat_ID;

                                            $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                                            if (isset($tag_extra_fields[$tag])) {
                                                $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                                                $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                                            }

                                        } else {

                                            $tag = $category[0]->category_parent;

                                            $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                                            if (isset($tag_extra_fields[$tag])) {
                                                $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                                                $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                                            }

                                        }





                                foreach($categories as $category) { ?>

Related posts

Leave a Reply