• I’m adding back blogging functionality to our design shop’s website. It’s an older custom WordPress template using WP as a CMS and the first I did mostly in house. So it’s a real hybrid mess of techniques. I am designer. But my php skills are weak.

    To have blog functionality I have to have the single.php template active, which I had previously deactivated since we were not doing any traditional blogging.

    Now I need to add the single.php template back. BUT in the header.php I used the slug to define my body ID for the CSS selectors on any given page. Each page hasa unique BODY ID for both navigation and some custom post styles. I know. I know. All this probably should have gone in the functions.php. But it was easier at the time.

    Now the problem is getting my hacky php to recognize an additional way of finding the correct Body ID via the new blog category rahter than slug on the single.php template. What I’ve been trying doesn’t seem to be working. See the “blahg” section in the code below:

    <body id="<?php
    			$slug = $post->post_name;
    			$post_data = get_post($post->post_parent);
    			$pslug = $post_data->post_name;
    
    			$category = get_the_category();
    			$categoryName = $category[0]->cat_name;
    			echo $categoryName;
    
    			if ($slug=="what" || $pslug=="what") echo "what";
    			elseif ($slug=="who" || $pslug=="who") echo "who";
    			elseif ($slug=="where" || $pslug=="where") echo "where";
    			// STORE SECTION
    			elseif ($slug=="store" || $pslug=="store") echo "store";
    			// BLAHG SECTION
    			elseif ($slug=="blahg" || $pslug=="blahg") echo "blahg";
    			elseif($categoryName=="blahg") echo "blahg";
    			else echo "home";
    ?>">

    This PHP just returns the BODY ID “blahg” and doesn’t default the ‘else echo “home”:’

    I’m sure this something simple. It’s driving me crazy. Does this make sense?Anybody have any ideas?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Have you considered using the body_class for this.

    Moderator keesiemeijer

    (@keesiemeijer)

    Or use is_single() to target all single posts

    If you need only posts with a category “blahg” you can use this:

    if(is_single()){
    	global $post;
    	$categories = get_the_category($post->ID);
    	if($categories){
    	  foreach($categories as $category){
    	    $categoryName[] = $category->cat_name;
    	  }
    	}
    	if(in_array('blahg', $categoryName)){
    	echo 'blahg';
    	}
    
    }

    Thread Starter Todd Christensen

    (@todd-christensen)

    Thanks keesiemeijer

    I’m not familiar with the usage of that method.

    The re-design of the site we’re working will be implementing ‘body_class’ and more robust implementation of the newest WordPress release – which I know little about. The re-design is going to be semantic and responsive and all that fun stuff but it’s months away from being implemented. I’m still designing it and wrapping my head around all these new possibilities.

    For now I’m just trying to retro-fit this Frankenstein code into working until then. Unless working in conditional tags with body_class is easier than I think and can be explained to me like one would to a three year old.

    I was hoping something about this code here would be obvious enough to band-aid. Which may not be the case. It is kind of a hodgepodge.

    Moderator keesiemeijer

    (@keesiemeijer)

    Did you read my second post above yours? Maybe that will help.

    Thread Starter Todd Christensen

    (@todd-christensen)

    keesiemeijer,

    Interesting.
    Are you placing that in the functions.php, correct?

    Moderator keesiemeijer

    (@keesiemeijer)

    No, use it in stead of elseif($categoryName=="blahg") echo "blahg";
    You can also use elseif(is_single()){ ... rest of code .... }

    Thread Starter Todd Christensen

    (@todd-christensen)

    Oh. Thank you so much I’ll give it a try. there are so many band aids running around in here who knows. I was experimenting with everything. I’ll give it a shot.

    The site, btw, is

    http:www.quesinberry.com

    The “blahg” section is still hidden for now.

    Thread Starter Todd Christensen

    (@todd-christensen)

    HAH! You rule. I had to change a couple of things but that worked. Thank you so much. If you get to Seattle I will buy you a cupcake.

    https://www.quesinberry.com/wordpress/blahg/

    Moderator keesiemeijer

    (@keesiemeijer)

    I will try to remember that. I’m Glad you got it resolved.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Categories or Slugs: Changing the BODY ID CSS for single.php’ is closed to new replies.