WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 14 months ago

#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 (3)

18118.patch (800 bytes) - added by SergeyBiryukov 4 years ago.
18118.2.patch (878 bytes) - added by SergeyBiryukov 14 months ago.
18118.3.patch (1.0 KB) - added by SergeyBiryukov 14 months ago.

Download all attachments as: .zip

Change History (15)

comment:1 @johnbillion4 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 4 years ago by johnbillion (previous) (diff)

comment:2 @SergeyBiryukov4 years ago

Closed #18220 as duplicate.

comment:3 @DennisSmolek4 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..

comment:4 @DennisSmolek4 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;

@SergeyBiryukov4 years ago

comment:5 @SergeyBiryukov4 years ago

  • Keywords has-patch added

comment:6 follow-up: @obenland2 years ago

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

comment:7 @buffler2 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 2 years ago by buffler (previous) (diff)

@SergeyBiryukov14 months ago

comment:8 in reply to: ↑ 6 @SergeyBiryukov14 months 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.

@SergeyBiryukov14 months ago

comment:9 @SergeyBiryukov14 months ago

18118.3.patch adds sanitize_html_class().

comment:10 @SergeyBiryukov14 months ago

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

comment:11 @ericlewis14 months 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.

comment:12 @SergeyBiryukov14 months 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.

Note: See TracTickets for help on using tickets.