#18118 closed enhancement (wontfix)
Append page slug to body_class function
Reported by: | andrewsvg | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Themes | Keywords: | has-patch |
Focuses: | template | 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 (4)
Change History (19)
#3
@
13 years ago
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..
#4
@
13 years ago
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;
#6
follow-up:
↓ 8
@
11 years ago
If page-specific styling is the purpose of this ticket, why not use the post/page ID classes?
#7
@
11 years ago
There may be good reason not to worry about this... but I thought I'd just chime in to +1 - I've taken to adding a filter on body_class to achieve this same thing.
True, ID classes can be used just the same - but not having to add comments anywhere you might use them feels nicer to me. ".page.about-us" is sort of self-documenting, whereas ".page-id-###" should probably be accompanied by a comment explaining which page it is anywhere it's used...
#8
in reply to:
↑ 6
@
10 years ago
- Focuses template added
- Keywords commit added
- Milestone changed from Awaiting Review to 4.0
- Type changed from feature request to enhancement
Replying to obenland:
If page-specific styling is the purpose of this ticket, why not use the post/page ID classes?
A page can accidentally be deleted, and when you recreate it, you don't get the same styling due to different ID.
Just got bitten by this. Refreshed the patch.
#9
@
10 years ago
18118.3.patch adds sanitize_html_class()
.
#11
@
10 years ago
I think this is an antipattern.
Slugs are mutable by content editors. I've been bitten by implementing what's suggested here and having clients change slugs, then they lose styling on X page and not knowing why.
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.