#35164 closed enhancement (fixed)
Singular body class and main post post class
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.8 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Themes | Keywords: | has-patch has-unit-tests commit has-dev-note |
Focuses: | Cc: |
Description
I think it would be pretty useful to be able to style all singular posts with one selector. I thought it might be a good idea to output ".singular" using the body_class()
function. This would allow people to target all singular pages.
In addition, detecting the main post on the page would be useful if someone had some related posts on a singular page. So the class ".main-post" could by added using the post_class()
function.
I'm using this method via hooks in a theme so I can say: .singular .main-post {}
which allows me to style posts and pages with one selector and give a default look to all custom post types.
Attachments (3)
Change History (32)
#1
follow-up:
↓ 2
@
9 years ago
- Component changed from General to Themes
- Version trunk deleted
I'm +1 for singular
, it's added by twentyeleven and twentyfouteen so the need is demonstrated.
My reading of is_main_query
is it will always be true in the body, and if it isn't it's likely an indication the pre_get_posts
filter should be used. If it isn't true, it will have been altered in code at which point a body_class
filter can be used.
-1 on main-post
#2
in reply to:
↑ 1
@
9 years ago
Replying to peterwilsoncc:
My reading of
is_main_query
is it will always be true in the body, and if it isn't it's likely an indication thepre_get_posts
filter should be used. If it isn't true, it will have been altered in code at which point abody_class
filter can be used.
You're completely right, good point! The feature would still be useful if we could detect the post output by the main query. For example: I would like my single post and single page titles to be centered. However, if I retrieve related posts below, I wouldn't want them to be centered. Hence:
css .singular .main-post .entry-title{ text-align:center; }
That said, I agree that this may not be a big issue. If there is a related posts section underneath that would likely have a wrapper class which you would use to style things anyway.
#3
@
9 years ago
- Keywords has-patch added
Thinking the same as @peterwilsoncc, +1 on singular
, -1 on main-post
as I don't think it can be done in a reliable way.
#5
@
9 years ago
- Owner set to johnbillion
- Resolution set to fixed
- Status changed from new to closed
In 36112:
#6
@
9 years ago
Awesome, thanks everyone for your input! I still think main-post would be a great addition, although I see why it can't be done reliably. Do you think it would be worth creating a separate ticket for it, perhaps for further down the line?
Also, would .main-post be helpful? In my mind it would solve some problems, but I wouldn't call myself a master of HTML and CSS architecture so perhaps I'm over-thinking this :)
#7
@
9 years ago
This change broke the TwentyEleven theme, which made its own assumptions about a body_class
called singular
(see 36510). The fix for this is probably adjusting TwentyEleven. But I wanted to leave a pointer here over to the ticket noting this issue.
This ticket was mentioned in Slack in #core by jorbin. View the logs.
9 years ago
#10
@
9 years ago
- Keywords needs-patch added; has-patch removed
- Milestone changed from 4.5 to Awaiting Review
Reverted in [37249].
This ticket was mentioned in Slack in #core by ocean90. View the logs.
9 years ago
#15
@
6 months ago
I searched using wpdirectory.net and there are 11 themes in the theme directory that use is-singular, but none that use wp-singular.
#16
@
6 months ago
@poena In that case, it's probably safer to go with the wp-
given the last attempt to add the class broke sites already using the existing class. Also, WP using the wp
prefix on things is a good thing in my not so humble opinion...
This ticket was mentioned in PR #8077 on WordPress/wordpress-develop by @poena.
5 months ago
#18
- Keywords has-patch has-unit-tests added; needs-patch removed
Themes: Add wp-singular to the list of body classes when viewing a single post object.
wp-singular includes a wp prefix to reduce conflicts with existing classes.
Updates test_singular_body_classes in Tests_Post_GetBodyClass to include the new CSS class.
Trac ticket: https://core.trac.wordpress.org/ticket/35164
This ticket was mentioned in PR #8107 on WordPress/wordpress-develop by @rinkalpagdar.
4 months ago
#19
Trac ticket: #35164
---
This adds a "singular" class to a single page using body_class().
@audrasjb commented on PR #8107:
4 months ago
#20
Closing in favor of #8077.
#22
@
4 months ago
- Keywords commit added
- Status changed from reviewing to accepted
Hello there,
I tested the PR on both a classic and a block theme.
- It correctly adds
wp-singular
body class to single posts, pages and custom post types singular posts but doesn't add it to other screens. - Unit tests updates are looking good to me.
- I found one occurrence of
wp-singular
in a plugin, but it is a false positive. - I found 7 occurrences of
wp-singular
in themes, but all are false positive.
I think we are good to go with the proposed implementation.
#23
@
4 months ago
Test Report
Description: This report validates whether the indicated patch works as expected.
PR Tested: https://github.com/WordPress/wordpress-develop/pull/8077
Environment
WordPress: 6.8-alpha-20250103.035904
PHP: 7.4.31
Browser: Chrome 131.0.0.0
OS: Windows
Theme: Twenty Twenty-Five 1.0
MU Plugins: None activated
Actual Results
- Issue resolved with patch.✅
#24
@
4 months ago
Test Report
Description: This report validates whether the indicated patch works as expected.
PR Tested: https://github.com/WordPress/wordpress-develop/pull/8077
Environment
WordPress: 6.8-alpha-20250103.035904
PHP: 7.4.31
Browser: Chrome 131.0.0.0
OS: Windows
Theme: Twenty Twenty-Five
MU Plugins: None activated
Actual Results
- ✅ Issue Resolved With Patch.
@audrasjb commented on PR #8077:
4 months ago
#26
committed in https://core.trac.wordpress.org/changeset/59689
#28
@
8 weeks ago
Note that the dev note for this is drafted and part of the Miscellaneous Dev Note.
#29
@
8 weeks ago
- Keywords has-dev-note added; needs-dev-note removed
This has now been published as a section in the Miscellaneous developer changes in WordPress 6.8 dev note post.
Adds the singular body class and the main-post post class