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.
The error says this: “Trying to get properties on a non-object.” Here is the offending code:
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
<option value="<?php echo $option->term_id; ?>" <?php if ( $settings[$id] == $option->term_id) { echo 'selected="selected"'; }?>>
<?php echo $option->name; ?>
</option>
<?php }?> </select>
I can’t figure out what I’m doing wrong. The line that’s specifically causing the error is this:
<option value="<?php echo $option->term_id; ?>" <?php if ( $settings[$id] == $option->term_id) { echo 'selected="selected"'; }?>>
Thanks for any advice! 🙂
You’re doing this a couple of times:
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, perhapsget_categories()
is sometimes returningnull
orfalse
in a failure condition. Check for that before accessing$option
.Like so:
It would seem that
$option
is an array. Try:OK I see what you’re saying. Earlier in my code I have this (overly simplified, just to show the important parts):
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.
Execute it from action…i.e.: