WordPress的functions.php问题 [英] WordPress functions.php Problem
问题描述
functions.php文件中的函数是否可以从functions.php中调用另一个函数?我猜是的,这就是为什么我写下面的代码,但由于某种原因它不起作用。任何人都可以请检查出来,并帮助我。
我试着从register_sidebar()调用pageBarColor()。
谢谢。
<?php
if(function_exists('register_sidebar')){
register_sidebar(数组(
'before_widget'=>'< li class =sidebarModule>',
'after_widget'=>'< / li> <! - end module - >',
'before_title'=>'< h2 class =moduleTitle'.pageBarColor()。'>',
'after_title'=>'< / h2> ',
));
函数pageBarColor(){
if(is_category('3')){
returncolor1;
} elseif(is_category('4')){
returncolor2;
} elseif(is_category('5')){
returncolor3;
} elseif(is_category('6')){
returncolor4;
} elseif(is_category('7')){
returncolor5;
}
}
?>
问题可能在于当您调用 register_sidebar
Wordpress尚未执行确定 is_category
结果的代码。如果你在定义之后直接调用你的 pageBarColor
函数,你会发现它不会返回任何东西。解决这个问题的一种方法是挂钩到 dynamic_sidebar_params
过滤器(当您在 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');
Can a function inside the functions.php file call another function from within 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()
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
. If you try calling your pageBarColor
function straight after defining it you'll find it doesn't return anything. 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:
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');
这篇关于WordPress的functions.php问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!