Skip to content

Instantly share code, notes, and snippets.

@svebal
Last active October 10, 2017 11:51
Show Gist options
  • Save svebal/32f08ca07b737a76e224e107466d983f to your computer and use it in GitHub Desktop.
Save svebal/32f08ca07b737a76e224e107466d983f to your computer and use it in GitHub Desktop.
[WordPress] adding custom css classes to body or posts
<?php
/**
* to body of one page
* https://developer.wordpress.org/reference/functions/body_class/
**/
function svebal_custom_body_class_v1( $classes ) {
if ( is_page( 'page-1' ) ) {
$classes[] = 'own-class';
}
return $classes;
}
add_filter( 'body_class', 'svebal_custom_body_class_v1' );
/** to body of more pages **/
function svebal_custom_body_class_v2( $classes ) {
if( is_page( array( 'page-1', 'page-2', 'page-2' ) ) ) {
$classes[] = 'own-class';
}
return $classes;
}
add_filter( 'body_class', 'svebal_custom_body_class_v2' );
/**
* to a post
* https://developer.wordpress.org/reference/functions/post_class/
**/
function svebal_custom_post_class( $classes ) {
if( is_page( 'page-1' ) ) {
$classes[] = 'own-class';
}
return $classes;
}
add_filter( 'post_class', 'svebal_custom_post_class' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment