WordPress.org

Ready to get started?Download WordPress

Forums

<body class=""> (12 posts)

  1. kmaier
    Member
    Posted 4 years ago #

    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

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    Nope - body_class() is all there is. What is that you're trying to generate as a class?

  3. kmaier
    Member
    Posted 4 years ago #

    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!

  4. Mike Little
    Member
    Posted 4 years ago #

    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

  5. kmaier
    Member
    Posted 4 years ago #

    hmm i'm getting an error

    link to pge

    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!

  6. Mike Little
    Member
    Posted 4 years ago #

    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

  7. kmaier
    Member
    Posted 4 years ago #

    cool, i see it in there!

  8. kmaier
    Member
    Posted 4 years ago #

    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

  9. xdesi
    Member
    Posted 4 years ago #

    Try clearing any empty spaces at the top or bottom of your functions.php

  10. kmaier
    Member
    Posted 4 years ago #

    That did it! thanks

  11. kmaier
    Member
    Posted 4 years ago #

    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

  12. Mike Little
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.