Support » Fixing WordPress » [Plugin: Widget Logic] widget logic – how to limit to children of one particular parent?

  • Resolved juliageek



    I’ve been trying this for ages – I would like all the pages of a particular parent (including the parent) to contain the same set of widgets. I KNOW I can make this happen with the magnificent widget logic, but I can’t work out the code to use. Please, please help!

    It’s got to be something like:

    if ($post_parent->ID == 101)
    (so I’m trying to limit it to all the children of post id 101. But that doesn’t work….

    Also, similarly, if I assign a set of pages a custom field (eg category: employment) is there a way of defining the widgets for the sidebars of that category?

    I’m sorry to be so dense, but I’m new to php…. and I’m going a bit crazy.

Viewing 15 replies - 1 through 15 (of 23 total)
  • alanft


    suggests stuff like

    (is_page(‘learning’) || $post->post_parent==”56″)

    will work, the trick with widget logic (and i might get it to do this by default) will be that you need to add “global $post;” so you end up with

    global $post; return (is_page('learning') || $post->post_parent=="56");

    try that and let me know 🙂




    Thanks for getting back so quickly. This does work! Thank you! It makes a different widget turn up for children of 101 and children of 57 – great.

    I just don’t understand what the

    return (is_page('learning')

    means though, because it doesn’t seem to have any effect on the outcome except that I can’t make it work without it…..and whatever I put in place of ‘learning’ doesn’t have any effect, even if it is one of my custom fields

    Any ideas?




    well you may not need that bit i suppose – i was just copying it from that page of the codex for you as an example.

    the || is a logical ‘OR’ — so what that code is doing is: return TRUE if this is the page named X OR the post parent is ID Y



    Thanks – yes, and I’ve now whittled it down to what is needed:

    global $post; return ($post->post_parent==”56″);

    The explanation of the ‘OR’ helped a lot. I really should learn the basics before I plunge in 😉

    Great plug in!

    I need to have a similar function on my site whereby a widget appears only on a specified Category page, as well as all child, sub-child and individual post pages within that category. Is this possible?

    I tried the scripts mentioned in this thread and while I can get the widget to show up on ONLY the specified category page, it is not appearing on any of the sub-category or individual post pages (within that category).

    Right now I am using the following code:
    global $post; return (is_category(‘Mountain Bikes’) || $post->post_parent==”7″);

    Any ideas on how I can make my widget show on the sub-category or individual post pages? THANK YOU in advance for any help you can provide.

    is_category(x) takes care of the main category page (as you know)
    in_category(x) takes care of single posts that are in the category (or sub categories – i think)

    what’s tricky is sub-category pages, there isn’t a is_descendent_of_category(x)

    no, wait! digging in the wp-code i’ve found the following in wp-includes/category.php . try it out

     * Check if a category is an ancestor of another category.
     * You can use either an id or the category object for both parameters. If you
     * use an integer the category will be retrieved.
     * @since 2.1.0
     * @param int|object $cat1 ID or object to check if this is the parent category.
     * @param int|object $cat2 The child category.
     * @return bool Whether $cat2 is child of $cat1
    function cat_is_ancestor_of( $cat1, $cat2 ) {

    Thanks for the quick response Alan. Unfortunately I’m not entirely well-versed in PHP or conditional tagging so I’m a little lost on how to to interpret and utilize the information you’ve provided.

    I tried replacing the “is_category(x)” with “in_category(x)” but this caused my widget to disappear on all pages including individual posts.

    I’m also not sure how to use the category ancestor information as it pertains to tags for Widget Logic. Can you possibly give me an example of how I can use this information? What would the entire tag look like when calling on these child category id’s?

    Thank you again. I really want this to work as I feel Widget Logic offers an extremely valuable function (and should probably come standard in WordPress 🙂

    ok, no worries. here’s how you get it to work. you need it to return TRUE when you want the widget to appear and that’s under 3 different circumstances. in programming logic that’s when a OR b OR c are true, which you write

    a || b || c

    each of those could be a test like you find on eg

    When the archive page for Category 9 is being displayed

    Returns true if the current post is in the specified category id

    it’s the last one (is in a category that is an ancestor of a parent category) that’s hard to do. that function i found above will make it simpler, but you’d still have to write some PHP to make use of it, and i’ve not got the time to do that here right now, sorry. check back later. you never know, someone else might chip in with the code before me

    Maybe you can straighten me out as well.

    I want a widget to appear on the static home page, the “Markets” page and the children of the Markets page.

    is_page(‘home’) || is_page(‘services’) puts the widget on just the two pages.

    is_page(‘home’) || is_page(13) does the same thing

    is_page(‘home’) || global $post; return ($post->post_parent==”13″); returns an error message as follows:

    Parse error: syntax error, unexpected T_GLOBAL in (web site address)/wp-content/plugins/widget-logic/widget_logic.php(132) : eval()’d code on line 1

    What am I doing wrong? This is driving me crazy!

    global $post;

    is a separate statement to get you the value of $global used elsewhere in the WP PHP code – you can’t use it as part of a condition as you are doing there.

    what you want to do is

    return ((is_page('home') || ($post->post_parent=="13"))

    but you need to delare the global before that. put it together and you have:

    global $post; return ((is_page('home') || ($post->post_parent=="13"));


    I have a client who is using the blog as her website.
    I need to have all the widgets only show up on a page I named “blog”
    where she is going to be doing her posting from.
    What code would I enter to make that happen. I have tried everything listed in here and I keep getting a syntax error.

    This plugin is one of the most valuable I use – in particular to make on widget show on one category’s overview page and its posts, an other in an other category. Have a look at the different widgets (ads and the “Opplev Piemonte/Lombardia” widgets) showing up on our Piemonte and Lombardia pages.
    The php statement is crucial of course. I use:
    ‘is_category(7) || (is_single() && in_category(7))’
    (without the ticks)
    Thanks to Allan
    PS Widget Logic v044 works fine also on WP251

    The text in the ‘Widget logic’ field can currently be full PHP code.

    I’m still trying to get this to work. I am running WordPress 2.7, Thesis 1.3.2, Widget-Logic 0.4.4

    When I put ‘global $post; return ((is_page(‘home’) || ($post->post_parent==”13″));’ (without the ticks) into widget-logic, I get the following error message:

    Parse error: syntax error, unexpected ‘;’ in /(website address)/wp-content/plugins/widget-logic/widget_logic.php(132) : eval()’d code on line 1

    Can I split some part of the command to an external php file in order to get this to work?

    your brackets are unbalanced.

    ( ( is_page(‘home’) || ($post->post_parent==”13″) )

    you can remove one ( from the beginning there

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘[Plugin: Widget Logic] widget logic – how to limit to children of one particular parent?’ is closed to new replies.