Widget Logic and Subpages


  1. Barnaby says:

    This is EXACTLY what I needed! Thank you for sharing 🙂

  2. Thanks for sharing. With the logic above you would think this “NOT” statement would work:

    global $post; return (!is_page('hudson-river-sustainable-shorelines') || ($post->post_parent=="86"));

    Though only the parent page works.

    • Andrew says:

      David it seems like your code would show a widget if the page is not ‘hudson-river-sustainable-shorelines’ or if the post is a sub-page of page ID 86. If your page ‘hudson-river..’ is also a sub-page of ID 86 I think the widget will be displayed because you are using the OR operator which essentially says if one of the two statements is correct show the widget. Changing the operate to && should then hide the widget on the hudson river page.

  3. Matthias says:

    The testing for multiple pages and their sub-pages works great. How would you exclude one or more sub-pages?


    • Andrew Sepic says:

      I haven’t tested this, but I would alter the last if statement and add an AND operator to add in my exclude. I hope that helps!

      { if ((in_array($child, $ancestors)) && != is_page(‘page to exclude’) ) { $show_widgets = TRUE; break; } } return $show_widgets;

  4. Tushar says:

    Can you help me in writing a code where I want to display a widget with Widget Logic only on home page and not on child pages like domain.com/page/2/ and so on.

    Help would be appreciated.

    • Andrew Sepic says:

      Tushar, That is a pretty straightforward task, but it depends on if your homepage is a static page (that you set in WP settings) or if it’s the default blog homepage that WordPress ships with.

      Use is_home() to show a widget on the main blog page. Or use is_front_page() to show for the front page of your site.

      Check out WP conditionals reference page for more information if you’re still having issues. – https://codex.wordpress.org/Conditional_Tags

      • Hi,

        You can solve this using following condition:
        `is_home() && !is_paged()`

        Since you haven’t set any homepage in Dashboard > Settings > Reading, you’ll need is_home() [and not is_front_page()].
        Also for NOT showing the widget on the sub-pages, you’ll need !is_paged() in combination with is_home().

        Both of these are required to be true to fulfil your requirement and hence you need && condition.

Leave a Reply

Your email address will not be published. Required fields are marked *