Right way to add class to post in WordPress, add class to post in single or page or home page, etc, add custom classes to post_class.

How To Add Class To Post In post_class()

Very easy, using post_class filter, we will add class to post easily, but if you want to add class in single post only, or in post type page only, or in home page only, you need some conditions, like is_single() function, more conditions.

Add Class To Post

Just copy function code and paste it in your “functions.php” file and change “my-class” to your class name:

function WPTime_add_class_to_post($class){
    $class[] = "my-class"; // change "my-class" to your class name, for example "movie-post"
    return $class;
add_filter('post_class', 'WPTime_add_class_to_post');

If you want to add 2 classes or more, just enter space between class name, for example “movie-post film”.

Add Class In Single Post And Other

Use conditions functions in post_class filter:

function WPTime_add_class_in_some_conditions($class){
    if( is_single() ){
        $class[] = "single-post"; // in single post

    if( is_page() ){
        $class[] = "page-post"; // in post type page

    if( is_home() ){
        $class[] = "home-page"; // in home page

    return $class;
add_filter('post_class', 'WPTime_add_class_in_some_conditions');

Add Class By Option

function WPTime_add_class_by_option($class){
    if( get_option('some_option') ){
        $class[] = "some-class";
    return $class;
add_filter('post_class', 'WPTime_add_class_by_option');

Read how to add theme options page, and how to add custom options.