Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add a Class to my Body Tag using the Forum Title


  • Rackmire
    Participant

    @rackmire

    Hi,

    I’m trying to build a theme for my BBpress site and I would like to add a class to my body tag that changes depending on where the user is within the forum but only for within a forum area.

    For instance… On my BBpress installation, if I have these forums:

    • Green
    • Red
    • Blue
    • Yellow

    If the user view topics within the ‘Green’ forum/sub-forum it add the classname ‘green’ to the body tag i.e “<body class=”green”>” I would like it to use the Forum’s title that is set. Can somebody help?

    Preferably the code to be in functions.php or header.php / bbpress theme files. I don’t want to hack the bbpress core files.

    I want to do this so I can change the way it looks i.e background colour etc.

    And the Homepage stays the same: <body> (with no classname unelss set within the header.php of the theme file.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)

  • Lynqoid
    Participant

    @lynqoid

    This is what you will need to use to actually add a class to the body class. You could look at getting the post by the get_queried_object(); function in WordPress and then getting the title of that object. After that you could then use the sanitize_title_with_dashes($forum_title); to get a safe class name.

    Please note I haven’t fully tested the code below and i’m not sure the $forum_object->post_title; will work but it get’s you moving in the right direction 🙂

    
    <?php
    $forum_object = get_queried_object();
    $forum_title = $forum_object->post_title;
    $forum_class = sanitize_title_with_dashes($forum_title);
    ?>
    <body <?php body_class($forum_class); ?>>
    

    Good luck!


    Rackmire
    Participant

    @rackmire

    Hi Thanks for your support and fast reply. Much appreciated,

    Do I just paste this code in my header.php file?

    Thanks,


    Daniel Lemes
    Participant

    @daniel_iceman

    Sorry, this functions seems to return the topic title, not the forum title. I have ‘googled’ around and got this:

    $forum_object = get_queried_object();
    $forum_id = $forum_object->forum_id; // get the forum id, is it right?
    $forum_title = get_forum_name( $topic->forum_id ); // get forum title by id
    $forum_class = sanitize_title_with_dashes($forum_title); //sanitize

    Not working since get_forum_name doesn’t work outside bbpress (inside WordPress header). Any other idea on how get the forum title (not the topic) and use as body class on WordPress? Thank you.


    Daniel Lemes
    Participant

    @daniel_iceman

    Edit: sorry, i’ve noticed my mistake since use the title was what Rackmire wanted. I need the inverse: use the parent forum title as class. I’m using a custom page template (forum.php).

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar