views:

219

answers:

1

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";
    }
}
?>
+1  A: 

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');
Richard M
hey thanks for the reply, but i know nothing about actions, so could you please link me to a requisite page.Thanks for the answer..
Aayush
I tried this and I am calling dynamic_sidebar in my theme, but nothing changes.
Aayush
my bad! I am not calling the dynamic_sidebar in my sidebar.phpinstead I have this:<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()) : ?><li><!-- stuff shown here in case no widgets active --></li><?php endif; ?>and the rest of my sidebar is manually made.is there any other way to do the color changes.
Aayush
This page of the Wordpress codex explains actions and filters (although the `dynamic_sidebar_params` action is not actually documented): http://codex.wordpress.org/Plugin_API
Richard M
can you please explain what is $widgets in your code.
Aayush
It's an array, which is passed to the filter function by Wordpress, containing parameters for each widget in a sidebar.
Richard M
I have added an example for a static sidebar in my answer to your followup-question: http://stackoverflow.com/questions/2180689/change-widgets-css-class-names-for-different-category-pages-in-wordpress/2190529#2190529
Jan Fabry