(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.
If I understand your question correctly then use are asking why
is_home()
isfalse
when you are viewing the URL/tools/example-tool/
? If I understand your question the answer is simply that is_home() is nottrue
for Custom Post Types.Actually
is_home()
should never betrue
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()
andis_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 istools
, and define sidebars specifically for that post type. But if you must route everything through onesingle.php
then here are some functions that you could use in place ofis_home()
andis_single()
to achieve the expected results and you can store these in your theme’sfunctions.php
file (or one of of the files of a plugin):Taking your first code example above and applying these function would look like this:
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.