Adding custom body class to the custom archive pages



PHP Snippet 1:

function my_archive_class( $classes ) {
    if ( is_page_template( 'archive-one.php' ) ) {
        $classes[] = 'archive-one';
    }

    if ( is_page_template( 'archive-two.php' ) ) {
        $classes[] = 'archive-two';
    }
    return $classes;
}
add_filter( 'body_class', 'my_archive_class' );

PHP Snippet 2:

function my_archive_class( $classes ) {
    if ( is_page_template( 'templates/archive-one.php' ) ) {
        $classes[] = 'archive-one';
    }

    if ( is_page_template( 'archive-two.php' ) ) {
        $classes[] = 'templates/archive-two';
    }
    return $classes;
}
add_filter( 'body_class', 'my_archive_class' );

PHP Snippet 3:

function my_archive_class( $classes ) {
    if ( basename( get_page_template()) == 'archive-one.php') {
        $classes[] = 'archive-one';
    }

    if ( basename( get_page_template()) == 'archive-two.php') {
        $classes[] = 'archive-two';
    }
    return $classes;
}
add_filter( 'body_class', 'my_archive_class' );