Make WordPress Core

Opened 13 years ago

Closed 10 years ago

Last modified 8 years ago

#18118 closed enhancement (wontfix)

Append page slug to body_class function

Reported by: andrewsvg's profile 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)

18118.patch (800 bytes) - added by SergeyBiryukov 13 years ago.
18118.2.patch (878 bytes) - added by SergeyBiryukov 10 years ago.
18118.3.patch (1.0 KB) - added by SergeyBiryukov 10 years ago.
35445.patch (898 bytes) - added by sebastian.pisula 9 years ago.

Download all attachments as: .zip

Change History (19)

#1 @johnbillion
13 years ago

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 13 years ago by johnbillion (previous) (diff)

#2 @SergeyBiryukov
13 years ago

Closed #18220 as duplicate.

#3 @DennisSmolek
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 @DennisSmolek
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;

#5 @SergeyBiryukov
13 years ago

  • Keywords has-patch added

#6 follow-up: @obenland
11 years ago

If page-specific styling is the purpose of this ticket, why not use the post/page ID classes?

#7 @buffler
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...

Last edited 11 years ago by buffler (previous) (diff)

#8 in reply to: ↑ 6 @SergeyBiryukov
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 @SergeyBiryukov
10 years ago

18118.3.patch adds sanitize_html_class().

#10 @SergeyBiryukov
10 years ago

Perhaps post slugs would be a bit superfluous, page slugs seem enough for now.

#11 @ericlewis
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.

#12 @SergeyBiryukov
10 years ago

  • Keywords commit removed
  • Milestone 4.0 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I actually have second thoughts on this too. It's trivial to add the class via body_class filter if needed.

#13 @SergeyBiryukov
9 years ago

#33653 was marked as a duplicate.

#14 @ocean90
9 years ago

#35445 was marked as a duplicate.

Note: See TracTickets for help on using tickets.