$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>sidebar - If page or sub page not working as expected|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

sidebar - If page or sub page not working as expected

matteradmin10PV0评论

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 ) {
Post a comment

comment list (0)

  1. No comments so far