Advertisement
brooklyndesignstudio

Add Custom Body Class to Pages

Jun 16th, 2021
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.52 KB | None | 0 0
  1. add_filter( 'body_class', 'custom_body_class' );
  2. /**
  3.  * Add custom field body class(es) to the body classes.
  4.  *
  5.  * It accepts values from a per-page custom field, and only outputs when viewing a singular static Page.
  6.  *
  7.  * @param array $classes Existing body classes.
  8.  * @return array Amended body classes.
  9.  */
  10. function custom_body_class( array $classes ) {
  11.     $new_class = is_page() ? get_post_meta( get_the_ID(), 'body_class', true ) : null;
  12.  
  13.     if ( $new_class ) {
  14.         $classes[] = $new_class;
  15.     }
  16.  
  17.     return $classes;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement