sidebar - If page or sub page not working as expected

admin2025-06-05  2

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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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 ) {
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749080620a316193.html

最新回复(0)