Opened 23 months ago

Last modified 22 months ago

#18118 new feature request

Append page slug to body_class function

Reported by: andrewsvg Owned by:
Priority: normal Milestone: Awaiting Review
Component: Themes Version:
Severity: normal Keywords: has-patch
Cc:

Description

I actually do this already on all of my sites, and thought this would be an obvious, amazing thing to add to the body_class function. Pretty much what you'd expect; the page-slug gets appended to the body_class, allowing for page-specific styling.

<?php body_class($post->post_name . ' '); ?>

Attachments (1)

18118.patch (800 bytes) - added by SergeyBiryukov 22 months ago.

Download all attachments as: .zip

Change History (6)

Probably needs a prefix to the slug, eg page-my-page instead of just my-page. Might be an idea to do it for all hierarchical CPTs too.

Last edited 23 months ago by johnbillion (previous) (diff)

Closed #18220 as duplicate.

It wont need a prefix as the body tag already gets the post type as a part of the class object. you would just use .post.page-title {}
There are two ways, one as a functions.php add on, the other as a part of the core file. You would want to check if its post/page because they have different name functions.

There is actually an example of this somewhere in the codex..

this looks right:
http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/post-template.php
add to line 330:

$classes[] = $post->post_name;

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.