Skip to content

Instantly share code, notes, and snippets.

@krogsgard
Created June 20, 2012 16:57
Show Gist options
  • Save krogsgard/2960925 to your computer and use it in GitHub Desktop.
Save krogsgard/2960925 to your computer and use it in GitHub Desktop.
add a body class based on a post's custom field value
<?php
// check for '_my_custom_field' meta key on pages and page parents and add a body class if meta value equals 'some-value'
add_filter('body_class','krogs_custom_field_body_class');
function krogs_custom_field_body_class( $classes ) {
global $post;
if ( is_page() && ( 'some-value' == get_post_meta( $post->ID, '_my_custom_field', true ) || 'some-value' == get_post_meta( $post->post_parent, '_my_custom_field', true ) ) ) {
$classes[] = 'some-new-body-class';
}
// return the $classes array
return $classes;
}
?>
<?php
// check for a certain meta key on the current post and add a body class if meta value exists
add_filter('body_class','krogs_custom_field_body_class');
function krogs_custom_field_body_class( $classes ) {
if ( get_post_meta( get_the_ID(), '_my_custom_field', true ) ) {
$classes[] = 'some-new-body-class';
}
// return the $classes array
return $classes;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment