views:

48

answers:

2

Hello All,

I have this function I created to give my wordpress site a body tag...

   function give_body_class( $print = true )
{
    global $wp_query;
    $c = "";

    // Generic semantic classes for what type of content is displayed

    is_archive()        ? $c = 'archive_page'       : null;
    is_date()           ? $c = 'date_page'          : null;
    is_search()         ? $c = 'search_page'        : null;
    is_paged()          ? $c = 'paged_page'         : null;
    is_tree(2)      ? $c = 'type_1'         : null;         
    is_tree(42)     ? $c = 'type_2'         : null;         
    is_tree(55)     ? $c = 'type_3'                 : null;             
    is_tree(57)     ? $c = 'type_4'                 : null;                 
    is_home()           ? $c = 'front'                  : null; 
    is_404()            ? $c = 'error_404'              : null; 

    return $print ? print($c) : $c;
}

I use this to print out the body class name in the header which is why I'm telling it to print.

And I am also trying use a logical statement on another page which is working, however I'm not sure how to stop it printing the variable.

e.g

<?php if (give_body_class('type_1')) { echo 'active'; } ?>

returns:

"type_1active"

A: 

Just change the last line to

return $c;
Sam Dufel
That's all very well for this usage but what if elsewhere this function is being called, and the function argument of true is being passed correctly (i.e. the value of $c should be printed)? The function is supposed to either print out $c or return it depending on that parameter. You could argue that it's a confusing approach in the first place and it would be better simply to return the value of $c and decide whether to print it elsewhere of course.
Mark Chorley
Beyond debugging, a print statement has no business being in a logic function.
Sam Dufel
I also really question the way it's written. Can't say for sure without seeing all the other little functions, but I'm willing to bet that every one of those other functions which is called should just be part of this function; calling a total of 10 helper functions just to identify the type of something? Really?
Sam Dufel
@Sam I don't know if you've ever worked with WordPress, or to what extent, but that's just how it's done. It may not be the most efficient way, but WordPress isn't built purely for efficiency, it's built for flexibility - in this case, this eliminates the struggle of figuring out whether it's safe to upgrade every time a WP update is released. Since you're using core functions, you don't have to worry about updating any of your own code. That's a big part of developing for WP, as I see it. I use core functions whenever possible, even at the sacrifice of a bit of processing cost.
Gavin
@Sam (again), I whole heartedly agree with your first comment, and this is one thing I don't love about WP. Sometimes unclear and I agree that all printing should be handled when the function is called and not in the function itself.
Gavin
+1  A: 

Do you understand what parameters the function takes ?? It's only a boolean which determine if it must print the class or not, just pass false to not print !

MatTheCat