WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#7939 closed enhancement (fixed)

add body_class function

Reported by: kretzschmar Owned by:
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.7.1
Component: Template Keywords:
Focuses: Cc:

Description

Now that there is a dynamic class function for themes in the core (post_class), please add one for the body tag too (I would like to see something like the one of the sandbox theme).

Attachments (2)

7939.diff (4.6 KB) - added by ionfish 9 years ago.
7939.2.diff (622 bytes) - added by DD32 8 years ago.

Download all attachments as: .zip

Change History (21)

#1 @ionfish
9 years ago

  • Keywords needs-patch added
  • Milestone changed from 2.7 to 2.8

New features are being bumped to 2.8 now.

#2 @DD32
9 years ago

  • Component changed from General to Template
  • Type changed from defect (bug) to enhancement

+1 Any theme lovers feel like knocking something up?

(I just tried to use it and was shocked to find, that WP didnt actually have one..)

#3 @ionfish
9 years ago

Tarski has one, I'll run up a patch based on that.

@ionfish
9 years ago

#4 @ionfish
9 years ago

  • Keywords has-patch added; needs-patch removed

Ended up basing it on the Sandbox one since it was more comprehensive, converted it to the WP coding style. I took out a few things like the date/time based classes which seemed excessive and would require more functions, and added an rtl class for RTL languages.

Here's the Sandbox code for comparison.

http://code.google.com/p/sandbox-theme/source/browse/trunk/functions.php?r=265#20

Might revisit it later and add some stuff back in; let me know if there's anything you think it needs.

#5 @ryan
9 years ago

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

(In [10485]) body_class(). Props ionfish. fixes #7939

#6 @DD32
9 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Version set to 2.8

Ended up basing it on the Sandbox one

Hah, I nearly commented "I would've patched it with sandbox's one, But i absolutely hate it" :)

I dont have time to make up a patch, But could this get added to the start of the main If blocks? (Oh, It also needs a whitespace cleanup)

	if ( is_singular() )
		$classes[] =  $wp_query->post->post_name;

ie:

	if ( is_404() )
		$classes[] = 'error404';

	if ( is_singular() )
		$classes[] =  $wp_query->post->post_name;

	if ( is_single() ) {

Having the current posts's slug in there is very handy for styling purposes.

The only other thing to say, Is whats the use of all the the_post()/rewind_post()'s? Most (if not all) of the info needed is already there in $wp_query->post, same with $wp_query->get why not just use get_query_var(), /endrant (Re-opening for the post slug, thats all)

#7 @ionfish
9 years ago

  • Keywords needs-patch added; has-patch removed

I intended the patch as a first pass; I was planning on going through and replacing the calls to the_post()/rewind_post() when I had the chance (it is a strange idiom). I'll have a look at rolling your other suggestions into it when I have some time tomorrow.

#8 @DD32
9 years ago

I intended the patch as a first pass

I thought as much :)

It does need some optimization I think, But the same would be said no matter where it came from.. At least we've got something in trunk now, and something to polish up! *strips the body_class related things out of his themes*

#9 @DD32
9 years ago

(er.. I meant my custom body_class() i added in the other day.. :P)

#10 @jamescollins
9 years ago

The body_class() function currently adds the post id to the list of classes (eg postid-827).

Would it be possible to include another class for the page id (if a page is being displayed)?

eg: "pageid-828"

I could write a simple patch to do this, but I'm not sure if ionfish is still working on rewriting the function?

@DD32
8 years ago

#11 follow-up: @DD32
8 years ago

  • Keywords has-patch added; needs-patch removed

attachment 7939.2.diff added

Adds mine and jamescollins suggetions.

#12 in reply to: ↑ 11 @jamescollins
8 years ago

Replying to DD32:

attachment 7939.2.diff added

Adds mine and jamescollins suggetions.

Looks good DD32.

#13 @jamescollins
8 years ago

  • Keywords tested added

The 7939.2.diff patch has been tested and looks good to me.

#14 @jamescollins
8 years ago

  • Version changed from 2.8 to 2.7.1

Is there any chance of getting this committed in time for WP 2.8?

Thanks very much.

#15 follow-up: @ryan
8 years ago

See #8446 for why post_name can be problematic.

#16 in reply to: ↑ 15 @jamescollins
8 years ago

Replying to ryan:

See #8446 for why post_name can be problematic.

Good point Ryan.

I'm not overly concerned with the post name being included.

I think the page ID should definitely be added though.

#17 @ryan
8 years ago

(In [11054]) Add page-id-x class to body for pages. jamescollins. see #7939

#18 @ryan
8 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

Let's handle further tweaks in new tickets.

#19 @jamescollins
8 years ago

  • Keywords has-patch tested removed
Note: See TracTickets for help on using tickets.