简体   繁体   中英

Wordpress theme options variable scope

I have my theme options saved and in my functions.php file, I have:

$my_option = get_option('theme_options');

So, if in functions.php I insert:

echo $my_option['name'];

It will echo correctly. However, if I put the same echo line anywhere else in any other theme file, it will not work.

The only way I have been able to get it to work is if I put:

global $my_option;

at the top of every single file. I have seen many other themes that didn't need to do this. Why can't I get the theme options variable to work throughout my theme?

Simply Open your functions.php file and at the very top globalize the variable eg:

<?php 
global $my_option;

and this should be available anywhere in your theme files as log as you are not inside a function, if that is the case then you will have to globalize it again inside the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM