Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

(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.

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.

share|improve this question
    
I try also this tut justintadlock.com/archives/2010/04/29/… but same result my homepage sidebar is loaded on single page also,... –  user1147 Oct 14 '10 at 2:45
    
It just won't load sidebar in single.php,when printing ordinary post,it loads sidebar,but for custom types,it won't load sidebar –  user1147 Oct 14 '10 at 2:53
    
@user1147 I edited your question heavily to try to make it more clear (please review to make sure I didn't change the meaning.) One thing I didn't understand was when you said "when I open a single page to check post"; can you be more clear what you meant by "single page?" –  MikeSchinkel Oct 14 '10 at 6:04
    
Sorry,when open single post,to check it,sidebar for single post is not loaded,I don't have pages,just categories. –  user1147 Oct 14 '10 at 13:43
    
@user1147 - See my update below. –  MikeSchinkel Oct 14 '10 at 23:20
show 1 more comment

2 Answers

up vote 6 down vote accepted

Hi @user1147

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

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');
  }
  ....
?>
share|improve this answer
    
Yes,it helps,but did not solve the problem,I use latest post for front page(not static page),according to this codex.wordpress.org/Conditional_Tags I will only need to change sidebar.php to this if (is_front_page()) { dynamic_sidebar('frontpage-sidebar'); } if (is_single()) { dynamic_sidebar('single-post-sidebar'); } but this do not work,still on single page,where post is printed,sidebar from home page is loaded.I need to load different sidebars on different pages-posts,on home page first,on single second sidebar,... Tnx Mike –  user1147 Oct 14 '10 at 13:31
    
single page = single post –  user1147 Oct 14 '10 at 13:44
    
@user1147 - I've reread your comment several times and still can't understand what you are asking. Can you update your question with actual URLs, and screenshots if possible, and showing what content you want to show up where? –  MikeSchinkel Oct 14 '10 at 21:51
    
Sorry,thanks for your patience,it is on my localhost,I will try again to explain.I have register several sidebars:front-page-sidebar,single-post-sidebar,...,when I put calendar widget on front page sidebar it is working fine(calendar widget is loaded on fronpage),but when I put category widget on single post sidebar it is showing me again calendar widget(calendar widget is again loaded in single post,even I put category widget in this sidebar).When using standard post type,it is working fine,but when I use custom post type,it doesn't work. –  user1147 Oct 14 '10 at 22:37
    
@user1147 - Are you using a one single.php template for all post types? Have you considered creating a single-tools.php for other your tools custom post type instead? I know it does not answer your "Why?" question, but it may get you what you want? –  MikeSchinkel Oct 14 '10 at 22:48
show 4 more comments

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.

share|improve this answer
    
This is the correct-est answer. –  Samuel Hulick Aug 21 '12 at 18:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.