0

我在我的 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;

有人知道为什么这行不通吗?还有一种方法可以显示此类错误/警告吗?

4

4 回答 4

1

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

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

于 2014-12-24T18:11:13.823 回答
0

在 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' );      
    }
于 2015-02-16T19:48:09.523 回答
0

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

于 2014-12-24T22:09:10.580 回答
0

你可以试试这样的

$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 选项将为空)

于 2015-07-15T11:39:46.403 回答