Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #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 etc

    can 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;
    }
    #1180830

    Hey 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,
    Mike

    #1180854

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

    #1181258

    Hi,
    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-ids

    Best regards,
    Mike

    #1181347

    Thanks Mike – can be closed.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘custom class to html tag …’ is closed to new replies.