Created
June 20, 2012 16:57
-
-
Save krogsgard/2960925 to your computer and use it in GitHub Desktop.
add a body class based on a post's custom field value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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