I'm using the Widget Logic plugin to show widgets on certain pages using Wordpress Conditional Tags (or any general PHP code).

What I would like to do is only show a widget on child pages, not top level pages. I tried both of the following but they both return true on all pages (so the widget shows on all pages):

global $post; return ($post->post_parent != 0);


global $post; return ( is_page() && $post->post_parent );

Note I tried this for testing and the widget displays on page ID 61 and its children, not just on its children as I was expecting:

global $post; return ($post->post_parent=="61");

Any tips?


  • how about return ($GLOBALS['post']->post_parent != 0); ? – onetrickpony Jul 1 '13 at 19:07
  • I'm not sure you can use PHP like you are trying to with Widget Logic. I have used the plugin many times but never tried raw PHP. According to the Codex it should be is_page() && $post->post_parent but you may be constrained to the conditional tags. – Brooke. Jul 1 '13 at 19:08
  • Thanks guys but neither of those worked. One Trick Pony, the widget still shows on both parent and child. BandonRandon, it shows on neither. – Caroline Elisa Jul 2 '13 at 18:38
  • Your syntax is correct and works as expected with widget logic. global $post; return ( is_page() && $post->post_parent ); Double check that your pages are exclusively parent or child pages as you are expecting. – iyrin Jul 2 '13 at 23:01
  • Perhaps you have the eval function blacklisted (that plugin relies on this naughty function) – onetrickpony Jul 5 '13 at 0:47

You can find the answer on the plugin's page at wordpress.org. Select the tab 'Other Notes'. You will find it at the example list:

global $post;

// WP page that is a child of page 77
return in_array(77, get_post_ancestors($post)); 

Edit: As an alternative to your suggestions you may try

global $post;
return !empty(get_post_ancestors($post)); 

This is an expensive call and should better be used for testing only. But I suspect that there might be something wrong with your page hierarchy. Is it possible that your top level page is not on root level?

| improve this answer | |
  • Thanks jot but that doesn't work for me. Firstly, it still shows on both parent and child. Secondly, I really want a solution that does not have to have the parent page ID hardcoded, instead shows on any child page. – Caroline Elisa Jul 2 '13 at 18:43
  • Thanks for editing your answer jot! But the second option gives me a PHP error: PHP Fatal error: Can't use function return value in write context in ~\widget_logic.php(284) : eval()'d code on line 1 – Caroline Elisa Jul 23 '13 at 22:00

Apologies, turns out it was a problem with my theme - using get_posts to list child pages outside the loop without wp_reset_postdata. So a global $post; conflict I guess.

global $post; return ($post->post_parent != 0); works fine.

| improve this answer | |
  • Can you explain what the theme was doing to cause the problem? – s_ha_dum Jul 23 '13 at 22:26
  • Sure! It was using get_posts to list child pages outside the loop without wp_reset_postdata. So a global $post; conflict I guess. – Caroline Elisa Jul 24 '13 at 14:11
  • Edit that into the answer, please. These aren't discussion forums. The answer itself should have all of the relevant information. – s_ha_dum Jul 24 '13 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.