1

I searched far and wide (on here & Google), reading about how to have multiple single.php pages for different categories of posts. The consensus was to do something like this:

<?php
/*
    Template name: Single
*/
?>
<?php

$post = $wp_query->post;

if ( in_category('blog') ) {
    //echo 'category is 3 which is blog-single';
    include(TEMPLATEPATH . '/blog-single.php'); 
} 

elseif ( in_category('projects') ) {
    //echo 'category is 4 which is project-single';
    include(TEMPLATEPATH . '/project-single.php'); 
} 

else {
    //echo 'category is neither of the above so default is selected';
    include(TEMPLATEPATH . '/project-single.php');
}

?>

But for whatever reason this is not working for me. It always goes to the else statement when I click on a blog post of a project post. The page above is called single.php and the two other pages are named project-blog.php & blog-single.php exactly.

I attempted to comment out the include(TEMPLATEPATH.. and just echo some text, but it always would go straight to the else no matter which post I click on.

I have triple checked the the category ID is correct for each if statement (see image below).

I also tried to make it is_category instead of in_category as well as also trying to put the category name in place of the category_id (for example blog or projects).

What am I doing wrong? Here's a screen shot of the URL from when drilling into a Category showing that the category_id (tag_id) is indeed correct. I hope this is enough info...

http://i.imgur.com/obsIT1w.jpg

edit: I even tried this...

if ( in_category('blog') || is_category(blog) || is_category('blog') || in_category(blog) || is_category(3) || is_category('3') || in_category('3') || in_category(3) ) {

And I changed my permalinks to include the category in them. So now project-single is working but not blog...

  • 1
    Even though you're getting the post you're not technically IN 'The Loop' - try passing the post into the function like The Codex suggests - so something like this in_category('blog', $post) or in_category('blog', $post->ID) – Howdy_McGee Apr 1 '14 at 15:34
  • @Howdy_McGee thanks! I see what you're saying, that makes sense. Unfortunately it didn't do the trick. I tried both samples. But I will look into that idea and the docs. I was able to get the projects category to work by changing the permalink structure. So worst case scenario I can just do an IF ELSE with the ELSE being my blog category. – james Apr 1 '14 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.