0

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";
    }
}
?>
2
  • Please specify what doesn't work. Do you get any error messages? Commented Jan 29, 2010 at 10:12
  • No error messages. Just returns nothing, page loads with nothing changed. Actually a space that's because I've given it there. Commented Jan 29, 2010 at 12:31

1 Answer 1

2

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');
Sign up to request clarification or add additional context in comments.

6 Comments

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..
I tried this and I am calling dynamic_sidebar in my theme, but nothing changes.
my bad! I am not calling the dynamic_sidebar in my sidebar.php instead 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.
This page of the Wordpress codex explains actions and filters (although the dynamic_sidebar_params action is not actually documented): codex.wordpress.org/Plugin_API
I have added an example for a static sidebar in my answer to your followup-question: stackoverflow.com/questions/2180689/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.