【问题标题】:Redux Wordpress Framework enqueue styleRedux Wordpress 框架入队样式
【发布时间】:2014-12-24 00:21:17
【问题描述】:

我在我的 wordpress 主题中使用 redux 框架。

我的全局变量是$ua_options

所以我在config.php 中创建了一个开关:

array(
   'id'       => 'ua-enable-responsive',
   'type'     => 'switch',
   'title'    => __( 'Responsive', 'redux-framework-demo' ),
   'compiler' => 'true',
   'mode'     => false,
),

在我的functions.php 中,我想有条件地将文件排入队列:

if ($ua_options['ua-enable-responsive']) {
    wp_enqueue_style( 'mytheme-bootstrap', get_template_directory_uri() . '/css/bootstrap.css'      );
}
else {
    wp_enqueue_style( 'mytheme-non-responsive', get_template_directory_uri() . '/css/no-responsive.css' );      
}

但这似乎不起作用。无论我是否在后端打开开关,第二个文件 no-responsive.css 都会被加载。

我还在functions.php顶部调用了全局变量

global $ua_options;

有人知道为什么这不起作用吗?还有没有办法显示此类错误/警告?

【问题讨论】:

    标签: php wordpress redux-framework


    【解决方案1】:

    这里是 Redux 的首席开发人员。在加载此代码之前,您需要调用 Redux 配置。如果你不能这样做,那么你必须做一个 get_option('opt_name') 来获取值。

    基本上我打赌你会发现 $us_options 是空的,因为它还没有被创建。 ;)

    【讨论】:

      【解决方案2】:

      好的,我需要在脚本 enqueue 函数中调用全局变量。

      【讨论】:

        【解决方案3】:

        在 header.php 中试试这个。也解决这个问题

            if ($ua_options['ua-enable-responsive'] == '1') {
        // '1' true and '0' false .
                wp_enqueue_style( 'mytheme-bootstrap', get_template_directory_uri() . '/css/bootstrap.css'      );
            }
            else {
                wp_enqueue_style( 'mytheme-non-responsive', get_template_directory_uri() . '/css/no-responsive.css' );      
            }
        

        【讨论】:

          【解决方案4】:

          你可以试试这样的

          $redux_options = maybe_unserialize( get_option( 'xyz_redux', false ) );
          
          if( $redux_options['custom-js'] ) {
            add_action('wp_enqueue_scripts', 'load_custom_js');
          }
          

          其中xyz_redux是Redux选项的全局变量名,也是数据库中的选项名。

          $redux_options['custom-js']是redux字段的ID。

          根据问题应该是$redux_options['ua-enable-responsive']

          你需要写一个函数来让 ike 入队

          function load_custom_js() {
            wp_register_script('custom-js', 'path/to/js', array(), '1.0', true );
            wp_enqueue_script('custom_js');
          }
          

          我有一个问题要问首席开发人员,这不是浪费资源吗?我的意思是我需要调用它

          $redux_options = maybe_unserialize( get_option( 'xyz_redux', false ) );

          每次我想从 redux 选项中获得一些东西? (因为根据您的回答,redux 选项将为空)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多