Make WordPress Core

Opened 7 years ago

Last modified 7 years ago

#39493 new enhancement

Include custom post type slug as body class

Reported by: danielbachhuber's profile danielbachhuber Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch
Focuses: Cc:


If I have a custom post type product and a post with slug laptop, then I can create a single-product-laptop.php template to render markup specific to this product.

But, a template needs styles too. It would be easier to target my CSS specific to this template if single-product-laptop was included as a body class.

Attachments (2)

39493.diff (573 bytes) - added by MatheusGimenez 7 years ago.
add body class single-{post_type_slug}-{post_name}
39493.2.diff (2.2 KB) - added by SergeyBiryukov 7 years ago.

Download all attachments as: .zip

Change History (6)

7 years ago

add body class single-{post_type_slug}-{post_name}

#1 @MatheusGimenez
7 years ago

  • Keywords needs-patch removed

#2 follow-up: @SergeyBiryukov
7 years ago

Previously suggested in #18118, wontfixed as an "antipattern".

#3 in reply to: ↑ 2 @danielbachhuber
7 years ago

Replying to SergeyBiryukov:

Previously suggested in #18118, wontfixed as an "antipattern".

For this thread, @ericlewis's comment:

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.

I don't think Eric's comment is consistent with WordPress' existing behavior.

In the scenario I described initially, the template is loaded based on the post slug. If the slug were to change, then the template would no longer load.

#4 @SergeyBiryukov
7 years ago

  • Keywords has-patch added

39493.2.diff adjusts the patch from #18118 and includes unit tests.

CSS classes support Unicode characters, so ideally this should support multibyte post slugs in a way similar to #37655, but apparently #33924 is a blocker for that.

Note: See TracTickets for help on using tickets.