-
AuthorPosts
-
February 1, 2020 at 2:01 pm #1180501
is there a way to give a unique class to html tag ( or body ) by a difined list like
page-id-x add class “abc ” to body etccan i use the existing filter: avf_custom_body_classes for that.
i now did it this way – but is there an Enfold – easier way to get it via the filter above?
add_filter( 'body_class','my_body_classes' ); function my_body_classes( $classes ) { if ( is_page(3) ) { $classes[] = 'datenschutz'; } if ( is_page(310) ) { $classes[] = 'leistungen'; } if ( is_page(1041) ) { $classes[] = 'impressum'; } if ( is_page(1678) ) { $classes[] = 'kontakt'; } if ( is_page(1682) ) { $classes[] = 'ueber-uns'; } if ( is_page(2057) ) { $classes[] = 'aktuelles'; } return $classes; }
February 3, 2020 at 11:22 am #1180830Hey Guenter,
I once found a WP filter that added body classes to posts based on categories, but I didn’t find one better than what you have found,add_filter( 'body_class', 'add_category_class' ); function add_category_class( $classes ) { if( is_single() && has_category('news') ) $classes[] = 'news-cat'; return $classes; }
Best regards,
MikeFebruary 3, 2020 at 12:10 pm #1180854Thanks Mike – I’ll keep that in mind.
The reason why I want to do it this way is to make it easier for me when transferring a styled page to a new customer.
For example, if I think a contact page is finished and good, it is not necessary to go through the whole CSS for the page-id of the contact page, because then the contact page always has the ID: Contact in addition to the page ID.
All my css is referenced then to #top.kontakt etc. pp.
_
And it has the advantage that you can see directly to which page this refers to in the Quick css – without having to look up which page-id the page currently has.February 4, 2020 at 1:15 pm #1181258Hi,
I see, that is a good idea. I researched this a little and found this article that seemed similar to what you want to do.We knew that we can keep one thing the same on all of these pages which were page-slugs, so we decided to add page slugs in body class which allowed us to do all the customizations we wanted without any complications.
They posted this function:
//Page Slug Body Class function add_slug_body_class( $classes ) { global $post; if ( isset( $post ) ) { $classes[] = $post->post_type . '-' . $post->post_name; } return $classes; } add_filter( 'body_class', 'add_slug_body_class' );
I tested on my localhost and found this works for pages and posts, for example:
the post “hello world” = post-hello-world
the page “meet the team” = page-meet-the-team
and the slugs should be the same across different installs regradless of the page-idsBest regards,
MikeFebruary 4, 2020 at 4:46 pm #1181347Thanks Mike – can be closed.
-
AuthorPosts
- The topic ‘custom class to html tag …’ is closed to new replies.