Basically I've applied this way to show certain colors on pages like search.php or a list on home. What it does is it reads a variable that gets a field from WP, if that field is equal to a certain number then it adds a class name which in CSS has a color Now the field on a single page is there, though whenever I add the 'if system' that I applied on the other pages it doesn't add the class in the body on header.php. Now this works correctly on other pages(Not globally but individually) but not here for some reason.

On the header.php:

    //Body class
    if($mem_id == 1)          { $class =  'color1';    }
    if($mem_id == 2)          { $class =  'color2';    }         
    if($mem_id == 3)          { $class =  'color3';    }
    if($mem_id == 4)          { $class =  'color4';    }
    if($mem_id == 5)          { $class =  'color5';    }
    if($mem_id == 6)          { $class =  'color6';    }
    if($mem_id == 7)          { $class =  'color7';    }
    if($mem_id == 8)          { $class =  'color8';    }
    if($mem_id == 9)          { $class =  'color9';    }
<body <?php body_class($class); ?>>

In other pages: $mem_id = get_field('memtype_id');

  • Note that if used as shown in the question, it would be better to use if/elseif/else to not add more than one class and to have a fallback and a hierarchy implemented. – kaiser May 7 '14 at 11:50
  • 1
    David, you could also use switch/case, but it looks like you already got a good solution from @kaiser – birgire May 7 '14 at 12:16

Actually you just need a callback for the body_class filter. Then simply check what correlation you got between your meta data and the actual value: If it is a 1:1 correlation, you can just add the result of get_field() as new body class. Example: array( get_field( 'mem_id' ) ) if the mem_id meta value would be color3 for e.g. If not, then you can add it as a sprintf() argument and build your class like in the example below. Keep in mind to check if you actually got a value and if not, set a default.

add_filter( 'body_class', function ( $wp, $extra )
    return array_merge( $wp, (array) $extra, array(
            "" !== get_field( 'membertype_id' )
                ? get_field( 'membertype_id' )
                : "1" // default
        # additional declarations here
    ) );
}, 20, 2 );

This is just an example and you surely can be much more sophisticated in crafting a solution. Just try to not bind it too hard to the actual value or field name as such dependencies can quickly become a maintenance nightmare.

Also keep in mind that the only reason I used get_field() from the "AdvancedCustomFields" plugin as this was the tag originally added to the question. In real WordPress this would be get_post_meta() or get_post_custom() or similar (depending on what you exactly are retrieving).

| improve this answer | |

Since you are specifying the class based off a value set elsewhere it seems you may have set this variable in another file than header.php you may have a scoping issue. I'd recommend in your functions.php file creating a global scope of your variable: global $mem_id; and then ensure that you internalize scope the global into each file you modify or use the value.

Also at first glance it seems what you are trying to accomplish what may exist in a plugin already http://wordpress.org/plugins/custom-classes/ (don't worry it hasn't been updated in a while but the logic works fine for current versions of WordPress). Just might want to check it out.

| improve this answer | |
  • 1
    I later also found out that the solution I was looking for was the is_single() function, but Kaiser's solution seemed more neat. I think the plugin is to much for what i'm trying to accomplish. – David H May 7 '14 at 13:31
  • 1
    After reviewing the code of the plugin in question, this is what the plugin does: Add a meta box to allow adding strings of custom classes, then add them in a callback to the body_class filter. For future questions: Please take the time to actually show a bit of code or copy/paste it from the plugin. If the plugin is gone/removed, your answer would be of no help for later visitors seeking help anymore. Thanks. – kaiser May 7 '14 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.