[英]Pass variable to wordpress php function
我从wordpress主题选项中有一个用于所选配色方案的变量:
$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' />
传递该变量的正确方法是什么?
您可以在函数内使用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');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.