Ticket #7939 (closed enhancement: fixed)

Opened 3 years ago

Last modified 3 years ago

add body_class function

Reported by: kretzschmar Owned by: anonymous
Priority: normal Milestone: 2.8
Component: Template Version: 2.7.1
Severity: normal Keywords:
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

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

Change History

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

New features are being bumped to 2.8 now.

comment:2   DD323 years ago

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

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

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

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

comment:5   ryan3 years ago

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

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

comment:6   DD323 years ago

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

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)

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

comment:8   DD323 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*

comment:9   DD323 years ago

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

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?

DD323 years ago

comment:11 follow-up: ↓ 12   DD323 years ago

  • Keywords has-patch added; needs-patch removed

attachment 7939.2.diff added

Adds mine and jamescollins suggetions.

comment:12 in reply to: ↑ 11   jamescollins3 years ago

Replying to DD32:

attachment 7939.2.diff added

Adds mine and jamescollins suggetions.

Looks good DD32.

  • Keywords tested added

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

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

comment:15 follow-up: ↓ 16   ryan3 years ago

See #8446 for why post_name can be problematic.

comment:16 in reply to: ↑ 15   jamescollins3 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.

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

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

Let's handle further tweaks in new tickets.

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