• Sandbox 1.6 – function.php hack

    I wanted to have different looks for different categories and also wanted child category single pages to have the same look as their parent pages on a design I’ve been working on.

    I also didn’t want to create extra category.php files, but instead control the looks primarily with CSS.

    The Sandbox template adds loads of useful classes do different html elements, including the body tag. But is was missing a parent category class. To include this open functions.php and replace:

    // Special classes for BODY element when a single post
    	if ( is_single() ) {
    		$postID = $wp_query->post->ID;
    		the_post();
    
    		// Adds 'single' class and class with the post ID
    		$c[] = 'single postid-' . $postID;
    
    		// Adds classes for the month, day, and hour when the post was published
    		if ( isset( $wp_query->post->post_date ) )
    			sandbox_date_classes( mysql2date( 'U', $wp_query->post->post_date ), $c, 's-' );
    
          // Adds category classes for each category on single posts
          if ( $cats = get_the_category() )
              foreach ( $cats as $cat )
                  $c[] = 's-category-' . $cat->slug;
    
    		// Adds tag classes for each tags on single posts
    		if ( $tags = get_the_tags() )
    			foreach ( $tags as $tag )
    				$c[] = 's-tag-' . $tag->slug;

    with:

    // Special classes for BODY element when a single post
    	if ( is_single() ) {
    		$postID = $wp_query->post->ID;
    		the_post();
    
    		// Adds 'single' class and class with the post ID
    		$c[] = 'single postid-' . $postID;
    
    		// Adds classes for the month, day, and hour when the post was published
    		if ( isset( $wp_query->post->post_date ) )
    			sandbox_date_classes( mysql2date( 'U', $wp_query->post->post_date ), $c, 's-' );
    
          // Adds category classes for each category on single posts
          if ( $cats = get_the_category() )
              foreach ( $cats as $cat )
                  $c[] = 's-category-' . $cat->slug;
    
          // Adds category classes for each category parent on single posts
          if ( $category = get_the_category() ) {
                  $p_cats = get_category($category[0]->category_parent);
    			  the_post();
    			  if ($p_cats)
    			  $c[] = 'p-category-' . $p_cats->slug;
          }
    
    		// Adds tag classes for each tags on single posts
    		if ( $tags = get_the_tags() )
    			foreach ( $tags as $tag )
    				$c[] = 's-tag-' . $tag->slug;

    If there is a parent category, this code will add p-category-slug to the body tag. Example from real site:

    <body class="wordpress y2009 m04 d03 h13 single postid-15 s-y2009 s-m03 s-d05 s-h01 s-category-css-tricks p-category-design s-author-admin loggedin">

    This is really neat if you want to use different color schemes, header images etc. for different sections on your blog.

    I hope this info will be useful to others!
    Patricia

Viewing 1 replies (of 1 total)
  • Thread Starter Patricia

    (@sumsera)

    Hmmm… unfortunately it isn’t a perfect solution. It doesn’t really work well when a post has several child categories which belong to different parent categories. Then there’s only 1 parent category displayed – looks like the last one found.

    Too bad I’m not very good with PHP. If anyone knows how to improve this code snippet, I would be most grateful!

    // Adds category classes for each category parent on single posts
          if ( $category = get_the_category() ) {
                  $p_cats = get_category($category[0]->category_parent);
    			  the_post();
    			  if ($p_cats) {
    			  $c[] = 'p-category-' . $p_cats->slug;
    			  }
          }
Viewing 1 replies (of 1 total)
  • The topic ‘Sandbox: add parent category class to body tag on single page’ is closed to new replies.