Post class and Body class with WordPress
Classes in WordPress are important and commonly forgotten (in poor themes) elements. There are two main roles for classes, body class and post class. Of course we may get different post class set for each post.
Body class
body_class() is a shortcut for get_body_class() function. The easiest method to use it, is:
1 |
<body <?php body_class();?>> |
Of course we may add our custom class, to do that, just put it as a string or an array. You may do that like this:
1 |
<body <?php body_class('foo bar');?>> |
or like this:
1 |
<body <?php body_class(['foo','bar']);?>> |
The result will be absolutely the same.
Of course you may use get_body_class() function, but you need to do something more. Like this:
1 |
<body class="<?=join(' ',get_body_class('foo bar'))?>"> |
or like this:
1 |
<body class="<?=join(' ',get_body_class(['foo','bar']))?>"> |
Post class
This is the very similar situation, but you need to use post_class instead of body_class and get_post_class instead of get_body_class, however there is one different – optional $post_id parameter, because we may get any post’s class if we need it.