Development,  WordPress

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:

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:

or like this:

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:

or like this:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *