1

選択した配色のワードプレスのテーマ オプションからの変数があります。

$themecolor

次のコードを使用して、スタイルシートを適切にインクルードしたいと考えています。

<?php /* enqueue color stylesheet */ 

function add_color_style() {
wp_register_style( 'color_style', get_template_directory_uri().'/css/'.$themecolor.'.css', 'all' );
wp_enqueue_style('color_style');
}

add_action('wp_enqueue_scripts', 'add_color_style'); ?>

問題は、$themecolor 変数が関数に渡されていないため、出力が次のようになることです。

<link rel='stylesheet' id='color_style-css'  href='http://bbmthemes.com/themes/expression/wp-content/themes/expression/css/.css' type='text/css' media='all' />

このような代わりに:

<link rel='stylesheet' id='color_style-css'  href='http://bbmthemes.com/themes/expression/wp-content/themes/expression/css/lime.css' type='text/css' media='all' />

その変数を渡す適切な方法は何ですか?

4

1 に答える 1

2

関数内で使用することも、そのオプションがテーブルからのものである場合はその関数内でglobal $themecolor使用することもできます。$themecolor = get_option('themecolor');wp_options

これを行うこともできます...

add_action('wp_enqueue_scripts', function() use ($themecolor) {
    wp_register_style( 'color_style', get_template_directory_uri().'/css/'.$themecolor.'.css', 'all' );
    wp_enqueue_style('color_style');
});
于 2013-04-24T03:30:26.557 に答える