Archived topic
Feature Request: class input field on the Layout metabox.
7 replies · Started by Ash on October 31, 2021
Would be nice if you could add "class" input field to the Layout metabox on post editing screen.(any post type)
Since css does not have "parent" selector, ability of adding custom class to body tag would be useful to style certain pages.
Hi Ash,
The body tag has a lot of classes already. There's a unique class for the page or post itself (example: postid-1 or page-id-1), and there's already one for the post type(example: single-post) as well.
I'm not sure how this is necessary considering what's already added. Can you explain a bit more about it?
Let us know. :D
Using post id is a bad practice I think.
If I duplicate it or recreate as different post, the style won't be inherited.
Also it's not convenient way to style multiple pages at once.
Using post id is a bad practice I think.
Not exactly. If it's specific to a page then it's good because this particular class will never change. It's assigned to the post as its unique identification number (hence postid).
If you're targeting post types, there's already one for that as I've mentioned on the previous post.
This article explains the rest of the classes being added by WordPress by default.
https://webdesign.tutsplus.com/tutorials/adding-to-the-body-class-in-wordpress--cms-21077
And with the filter, we can actually create own on "meta field" for body classes using ACF.
Example: Assuming you've already created a additional_body_class ACF field.
add_filter( 'body_class','add_body_classes' );
function add_body_classes( $classes ) {
$addclass = get_field('additional_body_class');
if($addclass){
$classes[] = $addclass;
}
return $classes;
}
which will add class added to the ACF field additional_body_class.
Ok, it was not proper word - bad practice.
But clients often ask page redesign without using a staging site so I have to duplicate or recreate the page.
Using post id is very inconvenient. it will cause headache every time I redesign.
it's "bad" at least for me in some situation.
It's possible by using additional custom field like you said.
but I thought there are many people who is in the same situation out there.
So this cloud be useful.
Using post id is very inconvenient. it will cause headache every time I redesign.
It's meant for styling a single specific page. If you're styling multiple pages or post then you shouldn't use post ids. You should use post types or taxonomy classes. :D
I do recognize specific situations like having a handy body class for specific pages so you don't have to list CSS like this:
body.postid-1 .entry-content p a,
body.postid-2 .entry-content p a,
body.postid-3 .entry-content p a, {
}
and do it cleanly w/ an assigned body class to each post with this:
body.assigned-class .entry-content p a {}
but since there are many ways of doing this (programmatically w/ WP Core filter body_class + is_page(), through Hook elements + display rule, ACF + body_class filter, etc ), this may be taken as a niche feature.
I've logged it as a feature request for you but since niche features like this one rarely gets requested so it we can't guarantee it gets implemented.
That said, if there's an immediate need for something like this, you can use the workaround for now. :D
Understand, thank you.
No problem. Glad to be of any help. :D