tags:

views:

55

answers:

1
    $skin_type = "green";

$category_id = get_cat_ID( single_cat_title("", false));
if($category_id == 218){
$skin_type = "blue";
}

I have this code at the top of my header to select different backgrounds for my categories. It works great when navigating between Categories with the menu, but it single_cat_title() does seem to work when I click a post to enter the single.php . What am I doing wrong and how can I get around it?

Thanks

+1  A: 

single_cat_title() works on category pages because the category is included in the query vars. On a single page, there isn't a category name or id included in the query vars, just the post id.

to take a look at the query vars, include the following in your page, either in the header or on category.php and in single.php to see the difference.

<?php var_dump($wp_query->query_vars);?>

You'll want to use get_the_category($post_ID) to get the categories of the single post. The function returns an array of objects, one for each category assigned to the post. To grab the first, or only category, use the array index "0". If used outside the loop, pass the post id to the function. Inside the loop, it will default to the current post id. Since you are controlling your header with this function, I bet you'll be passing the post id.

$skin_type = "green";

$post_id = $_GET['p'];    
$category = get_the_category($post_ID);
$category_id = $category[0]->**cat_ID**;

$if($category_id === 218){
    $skin_type = "blue;
}

Edit:

I realize I was assigning the category name to $category_id above, I've changed the object property to cat_ID which is the proper property

Udate to answer question in comment

If post "SFSF" is listed in categories 1,2,3, and 4, and you want to change the color of category 3 when the post is viewed on single.php, then you must add the category ID to the link that you click, assuming your navigation is category based. When you click on category 3 in your navigation menu, you'll see in the query string

"www.example.com/cat=3" 

Let's assume this is the title of the post.

<a href = "<?php the_permalink();?>/my_cat=<?php echo $_GET['cat'];?>">
    <?php the_title();?>
</a>

Here, you've added the custom query var "my_cat" and assigned it the value of the current page's category id.

On single.php, Instead of getting the category ID from the category name like I show above, you'll retrieve it from the query string similar to the way you retrieve the post ID.

$skin_type = "green";

$category_id = $_GET['my_cat'];    

$if($category_id === 218){
    $skin_type = "blue;
}
kevtrout
Thanks. but what if I want to get the category_id of the category where the post was clicked? so let's say post "sfsf" belonged to 1,2,3, and 4, and was clicked in 3. I want the single post to stay on 3 instead of going to 1.
Adam
I answered your question by editing this answer. Let me know if I can help more.
kevtrout
let me try it. I didn't know you could place GET vars in the url of permalinks
Adam