Wordpress - is_home() and is_single() Not Working as Expected with Custom Post Types?
If I understand your question correctly then use are asking why is_home()
is false
when you are viewing the URL /tools/example-tool/
? If I understand your question the answer is simply that is_home() is not true
for Custom Post Types.
Actually is_home()
should never be true
except for 1.) when on the home page list of posts, or 2.) when a "static page" has been set to be a "Posts page" in the Settings > Reading section of the admin (In my screen shot my "Posts page" has been set to a "Page" -- post_type=='page'
-- whose Title is "Home"):
(source: mikeschinkel.com)
So if you want the sidebar to show up I think you'll need to use a different criteria than is_home()
. Can you describe in words what you were trying to accomplish this code?
UPDATE
Based on the comments below and subsequent research after better understanding the problem it appears appropriate values for is_home()
and is_single()
were never really defined for custom post types. So one of the better solutions to the problem is to create a post type specific theme template page, i.e. single-tools.php
if the post type is tools
, and define sidebars specifically for that post type. But if you must route everything through one single.php
then here are some functions that you could use in place of is_home()
and is_single()
to achieve the expected results and you can store these in your theme's functions.php
file (or one of of the files of a plugin):
function is_only_home() {
$post_type = get_query_var('post_type');
return is_home() && empty($post_type);
}
function is_any_single() {
$post_type = get_query_var('post_type');
return is_single() || !empty($post_type);
}
Taking your first code example above and applying these function would look like this:
<?php
if (is_only_home()) {
dynamic_sidebar('frontpage-sidebar');
}
if (is_any_single()) {
dynamic_sidebar('single-post-sidebar');
}
....
?>
The function you want to use:
is_singular($post_types)
where $post_types
is string/array of custom post types. The function returns true if a singular page is being displayed.