[英]WordPress functions.php Problem
Can a function inside the functions.php file call another function from within functions.php? functions.php文件中的函数可以从functions.php中调用另一个函数吗? I'm guessing yes and which is why I wrote the code below, but it doesn't work for some reason.
我猜是的,这就是为什么我在下面编写代码,但是由于某些原因,它不起作用。 Can anyone please check it out and help me.
任何人都可以检查出来并帮助我。
I tried calling pageBarColor() from register_sidebar() 我尝试从register_sidebar()调用pageBarColor()
Thanks. 谢谢。
<?php
if (function_exists('register_sidebar')) {
register_sidebar(array(
'before_widget' => '<li class="sidebarModule">',
'after_widget' => '</li><!-- end module -->',
'before_title' => '<h2 class="moduleTitle '.pageBarColor().'">',
'after_title' => '</h2>',
));
}
function pageBarColor(){
if(is_category('3')) {
return "color1";
} elseif(is_category('4')) {
return "color2";
} elseif(is_category('5')) {
return "color3";
} elseif(is_category('6')) {
return "color4";
} elseif(is_category('7')) {
return "color5";
}
}
?>
The problem is probably that when you call register_sidebar
Wordpress has not yet executed the code which determines the result of is_category
. 问题可能是当您调用
register_sidebar
Wordpress尚未执行确定is_category
结果的is_category
。 If you try calling your pageBarColor
function straight after defining it you'll find it doesn't return anything. 如果您在定义后
pageBarColor
尝试调用pageBarColor
函数,则会发现它不返回任何内容。 One way of working around this would be to hook into the dynamic_sidebar_params
filter (which is called when you call dynamic_sidebar
in your templates, assuming you do) and update your widget before_title
values, something like this: 解决此问题的一种方法是挂接到
dynamic_sidebar_params
过滤器(假设您在模板中调用dynamic_sidebar
时调用该过滤器),并更新小部件的before_title
值,如下所示:
function set_widget_title_color($widgets) {
foreach($widgets as $key => $widget) {
if (isset($widget["before_title"])) {
if(is_category('3')) {
$color = "color1";
} elseif(is_category('4')) {
$color = "color2";
} elseif(is_category('5')) {
$color = "color3";
} elseif(is_category('6')) {
$color = "color4";
} elseif(is_category('7')) {
$color = "color5";
}
if (isset($color)) $widgets[$key]["before_title"] = str_replace("moduleTitle", "moduleTitle ".$color, $widget["before_title"]);
}
}
return $widgets;
}
add_filter('dynamic_sidebar_params', 'set_widget_title_color');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.