<body class=””>
-
I’m currently using the below code to change the body class on individual pages.
<body <?php body_class(); ?>>
is there a way to clean it up? say to what the permalink might be?
thanks for any help
kmaier
-
Nope – body_class() is all there is. What is that you’re trying to generate as a class?
bummer, just seeing if i could get the page name associated to it or make it custom. no biggie, i can work with it. it is just for css purposes
found that originally here from one of your other posts.
http://quirm.net/2009/08/07/changing-headers-with-wordpress-body_class/thanks!
Hi kmaier
There are already a lot of classes added to each body tag. For instance, on a page you get a class ‘page-id-99’ or whatever the page id is. That should allow you to customise each individual page.
However, if you would like the permalink slug in there, you can add the following to your functions.php
add_filter('body_class', 'my_body_class', 10, 4); function my_body_class($classes, $class, $comment_id, $post_id) { if (is_page()) { $classes[] = sanitize_title_with_dashes(get_the_title($post_id)); } return $classes; }
Hope that helps,
Mike
hmm i’m getting an error
i added the function to the file like this
<?php add_filter('body_class', 'my_body_class', 10, 4); function my_body_class($classes, $class, $comment_id, $post_id) { if (is_page()) { $classes[] = sanitize_title_with_dashes(get_the_title($post_id)); } return $classes; } ?>
thanks!
Sorry. try this
add_filter('body_class', 'my_body_class'); function my_body_class($classes) { if (is_page()) { $classes[] = sanitize_title_with_dashes(get_the_title()); } return $classes; }
That’s what I get for experimenting on a non-standard version 🙁
Mike
cool, i see it in there!
Hi Mike
do you know why i would be getting the following within the admin with that function?
Warning: Cannot modify header information – headers already sent by (output started at /home/paragon/public_html/dev/wp-content/themes/paragon/functions.php:25) in /home/paragon/public_html/dev/wp-includes/functions.php on line 784
Warning: Cannot modify header information – headers already sent by (output started at /home/paragon/public_html/dev/wp-content/themes/paragon/functions.php:25) in /home/paragon/public_html/dev/wp-includes/functions.php on line 785
Try clearing any empty spaces at the top or bottom of your functions.php
That did it! thanks
ran into something else…i have subpages for one of my sections but with this script it only takes the page and i have the section styled to that parent. is there a way to get the parent info in there too?
here is what i am running in the functions.php file
<?php add_filter('body_class', 'paragon_body_class'); function paragon_body_class($classes) { if (is_page()) { $classes[] = sanitize_title_with_dashes(get_the_title()); } return $classes; } ?>
thanks for any help
Try this:
<?php add_filter('body_class', 'my_body_class'); function my_body_class($classes) { global $post; if (is_page()) { $classes[] = sanitize_title_with_dashes(get_the_title()); if (isset($post->post_parent) && (intval($post->post_parent) > 0)) { $parent = get_post($post->post_parent); $classes[] = sanitize_title_with_dashes($parent->post_title); } } return $classes; } ?>
Mike
- The topic ‘<body class=””>’ is closed to new replies.