is_home() and is_single() Not Working as Expected with Custom Post Types?

(Moderators note: Was originally titled: “Custom post type problem?”)

I’m having some problems with custom post types where everything is working great except for the sidebars.

Read More

Here is some code from my sidebar.php:

<?php
  if (is_home()) {
    dynamic_sidebar('frontpage-sidebar');
  }
  if (is_single()) {
    dynamic_sidebar('single-post-sidebar');
  }
  ....
?>

Normally this works ok except for when I open a single page to check post 'frontpage-sidebar' is not loading as the 'single-post-sidebar' is loading instead. Where is the problem?

Here is the code for my custom post type:

$labels = array(
  'name' => _x('Tools', 'post type general name'),
  'singular_name' => _x('Tool', 'post type singular name'),
  'add_new' => _x('Add New', 'Tool'),
  'add_new_item' => __('Add New Tool'),
  'edit_item' => __('Edit Tool'),
  'new_item' => __('New Tool'),
  'view_item' => __('View Tool'),
  'search_items' => __('Search Tools'),
  'not_found' => __('No Tools found'),
  'not_found_in_trash' => __('No Tools found in Trash'),
  'parent_item_colon' => ''
);
$args = array(
  'labels' => $labels,
  'public' => true,
  'publicly_queryable' => true,
  'show_ui' => true,
  'query_var' => true,
  'rewrite' => true,
  'capability_type' => 'post',
  'hierarchical' => false,
  'menu_position' => 2,
  'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','page-attributes') // 'not sure that post can have page-attributes ????'
);
register_post_type('tools', $args);

How do I load different sidebars on different pages when using custom post types instead of ordinary posts?

Thanks.

Related posts

Leave a Reply

2 comments

  1. 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”):

    Setting a Home Page in WordPress 3.0's Admin Console
    (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');
      }
      ....
    ?>