简体   繁体   中英

How to create a shortcode for Admin Theme Option - Wordpress

I am trying to create a shortcode for 2 of my theme's options. Basically I want to grabe my theme options to my shortcode so that I can display them anywhere on my posts something like this:

[ads_1] 
[ads_2]

However when I tried to get the options on my shortcode it wont work at all and its giving me an error

Parse error: syntax error, unexpected 'theme_settings' (T_STRING) in C:\\xampp\\htdocs\\themewp\\wp-content\\themes\\bots_final\\shortcodes.php on line 86

Here's my snippet for my shortcode where in I am trying to grab my theme option data:

add_shortcode('ads_1', function($atts){
    return '
    <div>
        <?php $options = get_option( 'theme_settings' ); ?>
        <?php echo $options['banner1']; ?>
    </div>';

});

I am trying to grab the options from my Theme options page. Check my theme options code:

<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>

Any idea what's the problem why I can't grab it?

You opened the <?php inside an open php tag:

add_shortcode('ads_1', function($atts){
  $html = '<div>';
  $options = get_option( 'theme_settings' );
  $html .= $options['banner1'];
  $html .= '</div>';
  return $html;
});

What do you get from get_option( 'theme_settings' ); ? It seems like your suggesting an array.. To avoid conflicts, I'd store your settings as 'mytheme_theme_settings' .

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