WordPress:nav_menu_link_attributes不接受变量作为属性



我正在尝试采用自定义颜色,由用户在定制器中选择,并将其作为html属性添加到由特定菜单输出的链接。

下面是我的代码:
// Gets the footer link color, if assigned by user.
if (get_theme_mod('theme_footer_link_color')) {
$footerLinkColorAttribute = 'color:' . get_theme_mod('theme_footer_link_color', 'default_value') . ';';
}
function add_customizer_link_color_attribute_to_footer_menu_links($atts, $item, $args){
if ($args->theme_location == 'footerNavLocation') {
$atts['style'] = $footerLinkColorAttribute;
}
return $atts;
}
add_filter('nav_menu_link_attributes', 'add_customizer_link_color_attribute_to_footer_menu_links', 10, 3);

我的问题是,style属性将$footerLinkColorAttribute作为纯文本,而不是包含用户设置的文本的变量。

我怎样才能把它作为一个变量?

我明白了。我在函数外部定义变量,但需要在函数内部定义它:

function add_customizer_link_color_attribute_to_footer_menu_links($atts, $item, $args){
if ($args->theme_location == 'footerNavLocation') {
// If there is a footer link color assigned in the Customizer, this gets it ready to be inserted as an attribute into the link menu link text.
if (get_theme_mod('lets_get_started_footer_link_color')) {
$footerLinkColorAttribute = 'color:' . get_theme_mod('lets_get_started_footer_link_color', 'default_value') . ';';
}
$atts['style'] = $footerLinkColorAttribute;
}
return $atts;
}
add_filter('nav_menu_link_attributes', 'add_customizer_link_color_attribute_to_footer_menu_links', 10, 3);

最新更新