admin管理员组文章数量:1024603
I have a sidebar that displays parent, child and grand-child pages if they exist, I would like this to work on my custom post types.
I have the following custom post types set up: Events, Blog & News - these are all grandchild pages of News & Events which is a child page of the About section:
- About
- News & Events
- Blog
- Events
- News
- News & Events
I need to display the sidebar when on the archive and single pages but I cannot figure out how to do it.
Blog, Events and News are set up as pages in the wordpress admin area and they have been assigned the archive template which calls/includes my sidebar.
How do I display the hierarchy of the page structure in the sidebar when on an archive page?
My sidebar code looks like:
<aside class="default-sidebar">
<?php if ( 0 == $post->post_parent ) { ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php } else {
$parents = get_post_ancestors( $post->ID );
$parent_title = apply_filters( "the_title", get_the_title( end ( $parents ) ) );
$parent_url = apply_filters( "the_permalink", get_the_permalink( end ( $parents ) ) );
?>
<h4><a href="<?php echo $parent_url; ?>"><?php echo $parent_title; ?></a></h4>
<?php } ?>
<button id="default-sidebar-toggle--js" class="default-sidebar-toggle">Pages in this section</button>
<div class="default-sidebar-toggle-menu" id="default-sidebar-toggle-menu--js">
<?php
if(!$post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
else {
if($post->ancestors) {
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}
}
if ($children) { ?>
<ul><?php echo $children; ?></ul>
<?php } ?>
</div>
The code above is versatile enough that it can be used on any page within the site, it just doesn't work on custom post type and single pages.
I have a sidebar that displays parent, child and grand-child pages if they exist, I would like this to work on my custom post types.
I have the following custom post types set up: Events, Blog & News - these are all grandchild pages of News & Events which is a child page of the About section:
- About
- News & Events
- Blog
- Events
- News
- News & Events
I need to display the sidebar when on the archive and single pages but I cannot figure out how to do it.
Blog, Events and News are set up as pages in the wordpress admin area and they have been assigned the archive template which calls/includes my sidebar.
How do I display the hierarchy of the page structure in the sidebar when on an archive page?
My sidebar code looks like:
<aside class="default-sidebar">
<?php if ( 0 == $post->post_parent ) { ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php } else {
$parents = get_post_ancestors( $post->ID );
$parent_title = apply_filters( "the_title", get_the_title( end ( $parents ) ) );
$parent_url = apply_filters( "the_permalink", get_the_permalink( end ( $parents ) ) );
?>
<h4><a href="<?php echo $parent_url; ?>"><?php echo $parent_title; ?></a></h4>
<?php } ?>
<button id="default-sidebar-toggle--js" class="default-sidebar-toggle">Pages in this section</button>
<div class="default-sidebar-toggle-menu" id="default-sidebar-toggle-menu--js">
<?php
if(!$post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
else {
if($post->ancestors) {
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}
}
if ($children) { ?>
<ul><?php echo $children; ?></ul>
<?php } ?>
</div>
The code above is versatile enough that it can be used on any page within the site, it just doesn't work on custom post type and single pages.
Share Improve this question asked Jun 29, 2017 at 14:05 Neelam KhanNeelam Khan 3057 silver badges22 bronze badges1 Answer
Reset to default 2To simplify things, first make your archives actual Archives - not Pages. That will help you later on in listing the correct content.
unregister_post_type('blog');
unregister_post_type('event');
unregister_post_type('news');
Unregistering the post type won't delete any content, it will just ensure that when you re-register them, the settings will take effect.
Tweak your register_post_type
calls which should look something like this now:
register_post_type('blog',
array(
... a bunch of settings ...
)
);
and adjust, or add, has_archive
and rewrite
. Change the "news-and-events" part to whatever your News and Events Page slug is:
register_post_type('blog',
array(
... a bunch of settings ...,
'has_archive' => 'news-and-events/blog',
'rewrite' => array('slug' => 'news-and-events/blog'
)
);
(do the same for all 3 post types, swapping out "blog" for the current post type)
At this point, you'll need to delete the Pages, so WordPress doesn't get confused about whether to display a Page or an Archive. You should also visit the Settings > Permalinks page in wp-admin. You don't need to change any settings; just visiting this page will force WP to update permalinks and ensure you can access the new Archives.
Then, create or edit archive.php
in your theme. (If you're not already using a child theme or a custom theme, stop and create a child theme, so that when the parent theme updates, you won't lose your edits.) You can now add a conditional so that whenever you are on a Blog, Event, or News archive, you see that sidebar.
// Check the main query to see if this is one of the archives we're targeting.
$current_post_type = $wp_query->query['post_type'];
// If so:
if($current_post_type == 'blog' || $current_post_type == 'event' || $current_post_type == 'news') {
// Display the sidebar. (edit 'name-of-your-sidebar')
if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('name-of-your-sidebar')): endif;
}
// Otherwise, do nothing here. Or, insert a different sidebar that will apply to all other archives, such as Post Categories.
It sounds like you don't have the sidebar set up as a dynamic widgetized sidebar. So, you could either replace the "dynamic_sidebar" call above with a hard-coded sidebar, or turn your post listing code into a widget. I would recommend that approach, as you can then more easily control where it appears on the site.
I have a sidebar that displays parent, child and grand-child pages if they exist, I would like this to work on my custom post types.
I have the following custom post types set up: Events, Blog & News - these are all grandchild pages of News & Events which is a child page of the About section:
- About
- News & Events
- Blog
- Events
- News
- News & Events
I need to display the sidebar when on the archive and single pages but I cannot figure out how to do it.
Blog, Events and News are set up as pages in the wordpress admin area and they have been assigned the archive template which calls/includes my sidebar.
How do I display the hierarchy of the page structure in the sidebar when on an archive page?
My sidebar code looks like:
<aside class="default-sidebar">
<?php if ( 0 == $post->post_parent ) { ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php } else {
$parents = get_post_ancestors( $post->ID );
$parent_title = apply_filters( "the_title", get_the_title( end ( $parents ) ) );
$parent_url = apply_filters( "the_permalink", get_the_permalink( end ( $parents ) ) );
?>
<h4><a href="<?php echo $parent_url; ?>"><?php echo $parent_title; ?></a></h4>
<?php } ?>
<button id="default-sidebar-toggle--js" class="default-sidebar-toggle">Pages in this section</button>
<div class="default-sidebar-toggle-menu" id="default-sidebar-toggle-menu--js">
<?php
if(!$post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
else {
if($post->ancestors) {
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}
}
if ($children) { ?>
<ul><?php echo $children; ?></ul>
<?php } ?>
</div>
The code above is versatile enough that it can be used on any page within the site, it just doesn't work on custom post type and single pages.
I have a sidebar that displays parent, child and grand-child pages if they exist, I would like this to work on my custom post types.
I have the following custom post types set up: Events, Blog & News - these are all grandchild pages of News & Events which is a child page of the About section:
- About
- News & Events
- Blog
- Events
- News
- News & Events
I need to display the sidebar when on the archive and single pages but I cannot figure out how to do it.
Blog, Events and News are set up as pages in the wordpress admin area and they have been assigned the archive template which calls/includes my sidebar.
How do I display the hierarchy of the page structure in the sidebar when on an archive page?
My sidebar code looks like:
<aside class="default-sidebar">
<?php if ( 0 == $post->post_parent ) { ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php } else {
$parents = get_post_ancestors( $post->ID );
$parent_title = apply_filters( "the_title", get_the_title( end ( $parents ) ) );
$parent_url = apply_filters( "the_permalink", get_the_permalink( end ( $parents ) ) );
?>
<h4><a href="<?php echo $parent_url; ?>"><?php echo $parent_title; ?></a></h4>
<?php } ?>
<button id="default-sidebar-toggle--js" class="default-sidebar-toggle">Pages in this section</button>
<div class="default-sidebar-toggle-menu" id="default-sidebar-toggle-menu--js">
<?php
if(!$post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
else {
if($post->ancestors) {
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}
}
if ($children) { ?>
<ul><?php echo $children; ?></ul>
<?php } ?>
</div>
The code above is versatile enough that it can be used on any page within the site, it just doesn't work on custom post type and single pages.
Share Improve this question asked Jun 29, 2017 at 14:05 Neelam KhanNeelam Khan 3057 silver badges22 bronze badges1 Answer
Reset to default 2To simplify things, first make your archives actual Archives - not Pages. That will help you later on in listing the correct content.
unregister_post_type('blog');
unregister_post_type('event');
unregister_post_type('news');
Unregistering the post type won't delete any content, it will just ensure that when you re-register them, the settings will take effect.
Tweak your register_post_type
calls which should look something like this now:
register_post_type('blog',
array(
... a bunch of settings ...
)
);
and adjust, or add, has_archive
and rewrite
. Change the "news-and-events" part to whatever your News and Events Page slug is:
register_post_type('blog',
array(
... a bunch of settings ...,
'has_archive' => 'news-and-events/blog',
'rewrite' => array('slug' => 'news-and-events/blog'
)
);
(do the same for all 3 post types, swapping out "blog" for the current post type)
At this point, you'll need to delete the Pages, so WordPress doesn't get confused about whether to display a Page or an Archive. You should also visit the Settings > Permalinks page in wp-admin. You don't need to change any settings; just visiting this page will force WP to update permalinks and ensure you can access the new Archives.
Then, create or edit archive.php
in your theme. (If you're not already using a child theme or a custom theme, stop and create a child theme, so that when the parent theme updates, you won't lose your edits.) You can now add a conditional so that whenever you are on a Blog, Event, or News archive, you see that sidebar.
// Check the main query to see if this is one of the archives we're targeting.
$current_post_type = $wp_query->query['post_type'];
// If so:
if($current_post_type == 'blog' || $current_post_type == 'event' || $current_post_type == 'news') {
// Display the sidebar. (edit 'name-of-your-sidebar')
if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('name-of-your-sidebar')): endif;
}
// Otherwise, do nothing here. Or, insert a different sidebar that will apply to all other archives, such as Post Categories.
It sounds like you don't have the sidebar set up as a dynamic widgetized sidebar. So, you could either replace the "dynamic_sidebar" call above with a hard-coded sidebar, or turn your post listing code into a widget. I would recommend that approach, as you can then more easily control where it appears on the site.
本文标签: singleDisplay custom post types in wplistpages
版权声明:本文标题:single - Display custom post types in wp_list_pages 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745621927a2159626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论