admin管理员组文章数量:1130349
I would like to display a specific dynamic sidebar the the "about" page and its children. From the codex, I have tried the following:
<?php
// Begin main loop
if ( have_posts() ): while ( have_posts() ):
the_post();
// If about page and about child pages
if ( is_page('about') && $post->post_parent > 0 ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
// End loop
endwhile;
endif;
?>
This doesn't work. What am I missing?
I've also tried the following. This works for grandparents and parents, but not children.
<?php
if (is_page (17) || (17 == $post->post_parent ) ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
?>
Here's a post object dump of a child page using <?php echo '<pre>' . print_r( $post, true ); ?>:
WP_Post Object
(
[ID] => 382
[post_author] => 1
[post_date] => 2018-08-15 17:02:56
[post_date_gmt] => 2018-08-15 16:02:56
[post_content] =>
[post_title] => Art
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => art
[to_ping] =>
[pinged] =>
[post_modified] => 2018-10-22 12:39:27
[post_modified_gmt] => 2018-10-22 11:39:27
[post_content_filtered] =>
[post_parent] => 378
[guid] => /?page_id=382
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
I would like to display a specific dynamic sidebar the the "about" page and its children. From the codex, I have tried the following:
<?php
// Begin main loop
if ( have_posts() ): while ( have_posts() ):
the_post();
// If about page and about child pages
if ( is_page('about') && $post->post_parent > 0 ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
// End loop
endwhile;
endif;
?>
This doesn't work. What am I missing?
I've also tried the following. This works for grandparents and parents, but not children.
<?php
if (is_page (17) || (17 == $post->post_parent ) ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
?>
Here's a post object dump of a child page using <?php echo '<pre>' . print_r( $post, true ); ?>:
WP_Post Object
(
[ID] => 382
[post_author] => 1
[post_date] => 2018-08-15 17:02:56
[post_date_gmt] => 2018-08-15 16:02:56
[post_content] =>
[post_title] => Art
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => art
[to_ping] =>
[pinged] =>
[post_modified] => 2018-10-22 12:39:27
[post_modified_gmt] => 2018-10-22 11:39:27
[post_content_filtered] =>
[post_parent] => 378
[guid] => http://domain.local/?page_id=382
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
Share
Improve this question
edited Dec 20, 2018 at 16:57
Sam
asked Oct 15, 2018 at 15:40
SamSam
2,2163 gold badges31 silver badges59 bronze badges
4
|
1 Answer
Reset to default 0You might need to get the about page's ID and use that in your comparison:
if ( is_page( 'about' ) || $about_page_id == $post->post_parent ) {
I would like to display a specific dynamic sidebar the the "about" page and its children. From the codex, I have tried the following:
<?php
// Begin main loop
if ( have_posts() ): while ( have_posts() ):
the_post();
// If about page and about child pages
if ( is_page('about') && $post->post_parent > 0 ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
// End loop
endwhile;
endif;
?>
This doesn't work. What am I missing?
I've also tried the following. This works for grandparents and parents, but not children.
<?php
if (is_page (17) || (17 == $post->post_parent ) ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
?>
Here's a post object dump of a child page using <?php echo '<pre>' . print_r( $post, true ); ?>:
WP_Post Object
(
[ID] => 382
[post_author] => 1
[post_date] => 2018-08-15 17:02:56
[post_date_gmt] => 2018-08-15 16:02:56
[post_content] =>
[post_title] => Art
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => art
[to_ping] =>
[pinged] =>
[post_modified] => 2018-10-22 12:39:27
[post_modified_gmt] => 2018-10-22 11:39:27
[post_content_filtered] =>
[post_parent] => 378
[guid] => /?page_id=382
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
I would like to display a specific dynamic sidebar the the "about" page and its children. From the codex, I have tried the following:
<?php
// Begin main loop
if ( have_posts() ): while ( have_posts() ):
the_post();
// If about page and about child pages
if ( is_page('about') && $post->post_parent > 0 ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
// End loop
endwhile;
endif;
?>
This doesn't work. What am I missing?
I've also tried the following. This works for grandparents and parents, but not children.
<?php
if (is_page (17) || (17 == $post->post_parent ) ) {
dynamic_sidebar('about');
}
else {
dynamic_sidebar('general');
}
?>
Here's a post object dump of a child page using <?php echo '<pre>' . print_r( $post, true ); ?>:
WP_Post Object
(
[ID] => 382
[post_author] => 1
[post_date] => 2018-08-15 17:02:56
[post_date_gmt] => 2018-08-15 16:02:56
[post_content] =>
[post_title] => Art
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => art
[to_ping] =>
[pinged] =>
[post_modified] => 2018-10-22 12:39:27
[post_modified_gmt] => 2018-10-22 11:39:27
[post_content_filtered] =>
[post_parent] => 378
[guid] => http://domain.local/?page_id=382
[menu_order] => 0
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
Share
Improve this question
edited Dec 20, 2018 at 16:57
Sam
asked Oct 15, 2018 at 15:40
SamSam
2,2163 gold badges31 silver badges59 bronze badges
4
- Shouldn't the widget area be outside of the loop? – Pim Commented Oct 15, 2018 at 15:44
-
@Pim Doesn't seem to make a difference. I've tried without
&& $post->post_parent > 0 )and it works perfectly. – Sam Commented Oct 15, 2018 at 16:36 -
This is probably a due to a misunderstanding in how global variables work in PHP versus normal ones. I'd also advise against bundling up stuff like
endwhile; endif;with the same indenting, or putting multiple things on the same line, it's a great way to make mistakes and it makes code harder to read – Tom J Nowell ♦ Commented Oct 15, 2018 at 16:45 -
My
endwhile; endif;aren't on the same line within my template. I just included a simpler version for my question. – Sam Commented Oct 15, 2018 at 16:49
1 Answer
Reset to default 0You might need to get the about page's ID and use that in your comparison:
if ( is_page( 'about' ) || $about_page_id == $post->post_parent ) {
本文标签: sidebarIf page or sub page not working as expected
版权声明:本文标题:sidebar - If page or sub page not working as expected 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749080596a2312989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


&& $post->post_parent > 0 )and it works perfectly. – Sam Commented Oct 15, 2018 at 16:36endwhile; endif;with the same indenting, or putting multiple things on the same line, it's a great way to make mistakes and it makes code harder to read – Tom J Nowell ♦ Commented Oct 15, 2018 at 16:45endwhile; endif;aren't on the same line within my template. I just included a simpler version for my question. – Sam Commented Oct 15, 2018 at 16:49