WordPress.org

Make WordPress Core

Opened 18 months ago

Last modified 18 months ago

#39493 new enhancement

Include custom post type slug as body class

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

Description

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 18 months ago.
add body class single-{post_type_slug}-{post_name}
39493.2.diff (2.2 KB) - added by SergeyBiryukov 18 months ago.

Download all attachments as: .zip

Change History (6)

@MatheusGimenez
18 months ago

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

#1 @MatheusGimenez
18 months ago

  • Keywords needs-patch removed

#2 follow-up: @SergeyBiryukov
18 months ago

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

#3 in reply to: ↑ 2 @danielbachhuber
18 months 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
18 months 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.