我正在使用 PHP 并从foreach{}
循环内部调用以下函数。该函数需要接受$subTheme
作为选项参数,因为其目的是避免不必要的代码重复(DRY,对吗?)。
所以,首先 - 这是功能:
/*
* CSS needed to apply the selected styles to text elements.
*/
function this_site_text_css( $subTheme = '' ) {
$GLOBALS['this_theme_mod']; // this is in global scope already
$themeVarSuffix = $subTheme['var_suffix'];
$themeClassName = $subTheme['class_name'];
$content_bg_color = $this_theme_mod['content_bg_color' . $themeVarSuffix ];
$page_bg_color = $this_theme_mod['page_bg_color' . $themeVarSuffix ];
$primary_color = $this_theme_mod['primary_theme_color' . $themeVarSuffix ];
$css = 'body' . $themeClassName . '{ /* special classes */ };'
return $css
}
还有更多的事情发生,但它相当乏味,只是将 CSS 连接为要返回的字符串。
它被这样称呼
$data = '';
$data .= this_site_text_css();
$subThemeArray = array(
'theme_a' => array(
'var_suffix' => '_theme_a',
'class_name' => '.theme-a',
),
'theme_b' => array(
'var_suffix' => '_theme_b',
'class_name' => '.theme-b',
),
);
foreach( $subThemeArray as $key => $theme ) {
$data .= this_site_text_css( $theme );
}
我收到一个 PHP 警告Illegal string offset 'class_name'
,我猜这是因为 PHP 不希望我$themeVarSuffix
在$themeClassName
. 我很确定有办法做到这一点,并且我已经搜索过,也许我没有搜索到正确的关键字,但任何帮助将不胜感激。