This topic has 5 replies, 3 voices, and was last updated 7 years by Radu.

  • Author
  • #174390
     Patagonia
    Participant

    Hi, I was wondering if you could help me , or even add this as a feature as others might enjoy it:

    I want to have a different header background (the one you can set in the Kleo Theme settings) per category.

    I noticed that the body class function outputs the page ID as well as the parent-id, but I have several hundred pages divided over several layers deep (i.e, home, child pages 1-8, with many grandchild and even grand-grandchild pages). The child pages each have their own category, shared with their respective child and grandchild pages.

    How can I have the body class output the category? That would be the easiest to add CSS for.

    Or alternatively, the grandparents and/or grandgrandparents page ID in addition to the parent ID?

    Many thanks,

    Cheers, P

     

    #174396
     Patagonia
    Participant

    PS: I wasn’t sure how to call the category as body class output, but did try two variations to get the top-parent ID. I added these variations to the theme’s functions.php

    COPY CODE
    /* Get the Page Slug to Use as a Body Class, this will only return a value on pages! */
    $class = '';
    /* is it a page */
    if( is_page() ) { 
    	global $post;
            /* Get an array of Ancestors and Parents if they exist */
    	$parents = get_post_ancestors( $post->ID );
            /* Get the top Level page->ID count base 1, array base 0 so -1 */ 
    	$id = ($parents) ? $parents[count($parents)-1]: $post->ID;
    	/* Get the parent and set the $class with the page slug (post_name) */
            $parent = get_post( $id );
    	$class = $parent->post_name;
    }
    

    (from WordPress codex, this didn’t work)

    And I tried

    COPY CODE
    add_filter( 'body_class', 'dc_parent_body_class' );
    function dc_parent_body_class( $classes ) {
        	if( is_page() ) { 
            	$parents = get_post_ancestors( get_the_ID() );
    			$id = ($parents) ? $parents[count($parents)-1]: get_the_ID();
    		if ($id) {
    			$classes[] = 'top-parent-' . $id;
    		} else {
    			$classes[] = 'top-parent-' . get_the_ID();
    		}
    	}
     
    	return $classes;
    }
    

    Which also did not work. Neither did add the top-parent ID, not sure why not as your code does output all kinds of body classes?

    #174503
     Kieran_SQ
    Moderator

    Hi,

    I am going to send this ticket onto one of our developers as I am unable to only print the single primary category as a class.

    They’ll be in touch either on Monday or Tuesday, thanks for your patience.

    Kieran.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Custom development requests can be sent to dev@seventhqueen.com, one of the development team will be happy to discuss your needs.

    #174735
     Radu
    Moderator

    Use this instead

    COPY CODE
    
    // Add category slugs as classes in posts
    add_action('wp', 'kleo_post_categiries_body_class_init', 999);
    
    function kleo_post_categiries_body_class_init(){
        if(is_singular()){
            add_filter('body_class', 'kleo_post_categories_body_class');
            function kleo_post_categories_body_class($classes){
                $postcategories = get_the_category();
    
                foreach($postcategories as $postcat) {
                    $pcat = 'catslug-'.$postcat->slug;
                    $classes[] = $pcat;
                }
    
    
                return $classes;
            }
        }
    }
    
    

    will Generate catslug-categoryslug

    replace category slug with the category slug…

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #174749
     Patagonia
    Participant

    Thanks! That works fine, much appreciated.

    #174836
     Radu
    Moderator

    Great

    Cheers
    R.

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
Viewing 6 posts - 1 through 6 (of 6 total)

The forum ‘General questions’ is closed to new topics and replies.

Log in with your credentials

Forgot your details?