Ticket #7939 (closed enhancement: fixed)
add body_class function
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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
Change History
- 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..)
- 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.
- Status changed from new to closed
- Resolution set to fixed
- 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.
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:10
jamescollins — 3 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?
comment:11
follow-up:
↓ 12
DD32 — 3 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
jamescollins — 3 years ago
Replying to DD32:
attachment 7939.2.diff added
Adds mine and jamescollins suggetions.
Looks good DD32.
comment:13
jamescollins — 3 years ago
- Keywords tested added
The 7939.2.diff patch has been tested and looks good to me.
comment:14
jamescollins — 3 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.
comment:15
follow-up:
↓ 16
ryan — 3 years ago
See #8446 for why post_name can be problematic.
comment:16
in reply to:
↑ 15
jamescollins — 3 years ago
comment:17
ryan — 3 years ago
comment:18
ryan — 3 years ago
- Status changed from reopened to closed
- Resolution set to fixed
Let's handle further tweaks in new tickets.


New features are being bumped to 2.8 now.