WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Widget Logic] Negating a parent and its children (5 posts)

  1. RickA99
    Member
    Posted 4 years ago #

    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?

  2. RickA99
    Member
    Posted 4 years ago #

    The correct answer appears to be:

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

  3. leet8845
    Member
    Posted 4 years ago #

    How would I also exclude the grandchilren?

  4. alanft
    Member
    Posted 4 years ago #

    you might find the 'get_post_ancestors' example on

    http://wordpress.org/extend/plugins/widget-logic/other_notes/

    is what you need

  5. leet8845
    Member
    Posted 4 years ago #

    Hi alan,

    I think you solved this for me over at freakytrigger.co.uk

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic