admin管理员组文章数量:1435859
(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.
(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 edited Oct 14, 2010 at 23:20 MikeSchinkel 37.6k14 gold badges117 silver badges132 bronze badges asked Oct 14, 2010 at 2:10 user1147user1147 8024 gold badges14 silver badges26 bronze badges 6- I try also this tut justintadlock/archives/2010/04/29/… but same result my homepage sidebar is loaded on single page also,... – user1147 Commented Oct 14, 2010 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 Commented Oct 14, 2010 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 Commented Oct 14, 2010 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 Commented Oct 14, 2010 at 13:43
- @user1147 - See my update below. – MikeSchinkel Commented Oct 14, 2010 at 23:20
2 Answers
Reset to default 7If 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)
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.
本文标签: customizationishome() and issingle() Not Working as Expected with Custom Post Types
版权声明:本文标题:customization - is_home() and is_single() Not Working as Expected with Custom Post Types? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673121a2669691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论