“Trying to get properties on a non-object” in PHP

前端 未结 4 1879
一向
一向 2021-01-05 10:43

I\'m a WordPress theme developer and on a theme I\'m working on, I\'ve turned on debug mode and get this error inside a select drop-down box for the theme\'s options page. <

相关标签:
4条回答
  • 2021-01-05 10:49

    OK I see what you're saying. Earlier in my code I have this (overly simplified, just to show the important parts):

    $news_categories = get_categories()  //pulls all of the categories from WordPress
    
            array ("minititle"      => '<span class="mini_title">Select the category you\'d like to use as your "Top Story" category.</span>',
                "id"        => $shortname . "_top_story_category",
                "type"      => "cat_select",
                "options" => $news_categories)
    

    This is where $value['options'] comes from. The code is part of a switch statement...

    I would have to post the entire file in order to give the context, and it's quite long.

    0 讨论(0)
  • 2021-01-05 10:52

    It would seem that $option is an array. Try:

    $option['term_id']
    
    0 讨论(0)
  • 2021-01-05 10:56

    You're doing this a couple of times:

    $option->someproperty
    

    In at least one of the cases $option is not an object. When you then do -> on a non-object, you get that error.

    First, verify that $option is actually an object and correct the usage if it is not.

    If $option is an array then @matthewpavkov is correct, you should do $option['someproperty'] instead of $option->someproperty.

    If $option is usually an object, perhaps get_categories() is sometimes returning null or false in a failure condition. Check for that before accessing $option.

    Like so:

    foreach ($value['options'] as $option)
    {
        if ($option)
        {
            // do whatever;
        }
    }
    
    0 讨论(0)
  • 2021-01-05 11:11

    Execute it from action...i.e.:

    function your_function(){
        YOUR CODES HEREEEEEEEEEEE............
    }
    ADD_ACTION('init','your_function');
    
    0 讨论(0)
提交回复
热议问题