Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#35164 reviewing enhancement

Singular body class and main post post class

Reported by: danielpataki's profile danielpataki Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords: needs-patch
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 (1)

post-template.diff (749 bytes) - added by danielpataki 8 years ago.
Adds the singular body class and the main-post post class

Download all attachments as: .zip

Change History (15)

@danielpataki
8 years ago

Adds the singular body class and the main-post post class

#1 follow-up: @peterwilsoncc
8 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 @danielpataki
8 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 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.

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:

.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.

Last edited 8 years ago by peterwilsoncc (previous) (diff)

#3 @swissspidy
8 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.

#4 @johnbillion
8 years ago

  • Milestone changed from Awaiting Review to 4.5

#5 @johnbillion
8 years ago

  • Owner set to johnbillion
  • Resolution set to fixed
  • Status changed from new to closed

In 36112:

Themes: Add singular to the list of body classes when viewing a single post object.

Adds tests

Fixes #35164
Props danielpataki, johnbillion

#6 @danielpataki
8 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 @eceleste
8 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.


8 years ago

#10 @ocean90
8 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from 4.5 to Awaiting Review

Reverted in [37249].

#11 @SergeyBiryukov
8 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

This ticket was mentioned in Slack in #core by ocean90. View the logs.


8 years ago

#13 @peterwilsoncc
8 years ago

Any interest in:

  • is-singular
  • wp-singular

or some such?

#14 @johnbillion
8 years ago

  • Owner johnbillion deleted
  • Status changed from reopened to reviewing
Note: See TracTickets for help on using tickets.