Support » Plugins » [Plugin: Widget Logic] Negating a parent and its children

  • Resolved RickA99


    The following logic works fine on the parent and children pages where a widget is supposed to appear:

    global $post; return (is_page(555) || ($post->post_parent==”555″));

    But I also want other widgets to not appear on these same pages. I tried the following but it blocks the widgets from appearing on all pages which is not OK while allowing it on the home (blog) page and single (post) pages which is OK.

    global $post; return (!is_page(555) && (!$post->post_parent==”555″));

    What am I doing wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • RickA99


    The correct answer appears to be:

    global $post; return !(is_page(555) || ($post->post_parent==”555″));

    How would I also exclude the grandchilren?

    you might find the ‘get_post_ancestors’ example on

    is what you need

    Hi alan,

    I think you solved this for me over at

    You said
    ““I want to exclude all the children of 219, 409 and 357″

    then you need to add || ($post->post_parent==”219″) in there too – which is doing the job of excluding the children of 219. the is_page(219) will also exclude page 219.

    when the code is getting a bit repetitious like this, you could try reformulating using PHP’s in_array like this:

    global $post; return !( in_array($post->post_parent, array(219,357,409)) );

    wordpress’s own is_page can take an array for multiple values too, so if you also wanted to exclude those parent pages:

    global $post; return !( is_page(array(219,357,409)) || in_array($post->post_parent, array(219,357,409)) );”

    I used ‘global $post; return !( is_page(array(219,357,409)) || in_array($post->post_parent, array(219,357,409)) )’

    It works perfect! Thanks again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: Widget Logic] Negating a parent and its children’ is closed to new replies.