Skip to content

Instantly share code, notes, and snippets.

@tmort
Last active May 17, 2019 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmort/4c14cead10354b296510f789de4fd0b5 to your computer and use it in GitHub Desktop.
Save tmort/4c14cead10354b296510f789de4fd0b5 to your computer and use it in GitHub Desktop.
WordPress custom class per page based on object name.
<?php
add_filter('body_class', 'sf3_custom_body_class');
function sf3_custom_body_class($classes){
global $post;
$prefix = 'cpgname_'; //insert your own prefix
if(isset($post->post_type)){ //if a post-type is required
$prefix .= sprintf('%s_%s', $prefix, $post->post_type);
// 'cpgname_page'
}
if(isset($post->post_name)){
$classes[] = sprintf('%s_%s', $prefix, $post->post_name);
// 'cpgname_page_contact-us'
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment