WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Widget Logic] widget logic - how to limit to children of one particular parent? (24 posts)

  1. juliageek
    Member
    Posted 5 years ago #

    Hi

    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.

  2. alanft
    Member
    Posted 5 years ago #

    http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

    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 :-)

  3. juliageek
    Member
    Posted 5 years ago #

    Hi

    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?

    Thanks

  4. alanft
    Member
    Posted 5 years ago #

    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

  5. juliageek
    Member
    Posted 5 years ago #

    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!

  6. cridge
    Member
    Posted 5 years ago #

    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.

  7. alanft
    Member
    Posted 5 years ago #

    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 ) {
  8. cridge
    Member
    Posted 5 years ago #

    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 :-)

  9. alanft
    Member
    Posted 5 years ago #

    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 http://codex.wordpress.org/Conditional_Tags eg

    is_category('9')
    When the archive page for Category 9 is being displayed
    ...

    in_category('5')
    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

  10. RickA99
    Member
    Posted 5 years ago #

    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!

  11. alanft
    Member
    Posted 5 years ago #

    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"));

    :-)

  12. Linda Lee
    Member
    Posted 5 years ago #

    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.

  13. alanft
    Member
    Posted 5 years ago #

  14. kjetilgf
    Member
    Posted 5 years ago #

    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
    Kj
    PS Widget Logic v044 works fine also on WP251

  15. RickA99
    Member
    Posted 5 years ago #

    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?

  16. alanft
    Member
    Posted 5 years ago #

    your brackets are unbalanced.

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

    you can remove one ( from the beginning there

  17. RickA99
    Member
    Posted 5 years ago #

    Actually, I copied the code I used from the WordPress - Plugin Directory - Widget Logic - Other Notes page that reads as follows:

    Examples:
    is_home()
    !is_category(5)
    is_single() && in_category('baked-goods')
    is_page('about')
    current_user_can('level_10')
    global $post; return ((is_page('home') || ($post->post_parent=="13"));

    After removing the extra bracket, IT WORKED! Thank you very much. I needed the logic to include the parent page so the final version reads likes this:

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

    Thanks again. Widget Logic is wonderful!

  18. RickA99
    Member
    Posted 5 years ago #

    OK, now I want to use Widget Logic to select a category page and all the posts in that category.

    When I use 'is_category(4)', it puts the widget on the Category page but not on the posts in that category.

    Oops! I answered my own question. This should work:

    'is_category(4) || in_category('4')'

  19. alanft
    Member
    Posted 5 years ago #

    oh good grief! i will correct that in the documentation! thank you.

  20. joshasbury
    Member
    Posted 5 years ago #

    I'm seeing some strange behavior which I just can't seem to work around. For example, say I have a main category page called "Countries", and I want an image to appear on that page only -- I add a text widget and put the condition of is_category('149'). That works like a champ. The image only appears on that page.

    I have a subcategory of India, and I want an Indian-specific image to appear on it's main page coupled with its individual posts -- so I add a text widget with this condition: is_category('188') || in_category('188'). That works like a champ.

    I created another subcategory for Mexico and want to do the same thing as India. So I create a new text widget with this condition: is_category('220') || in_category('220')

    The problem here is that the Mexico image is showing up on my main Countries page (India isn't). Any ideas what I might be doing wrong here? I'm scratching me head with this one...

  21. alanft
    Member
    Posted 5 years ago #

    my guess is that the first (or last) post showing on the countries page may also be in the mexico category?

    so if you change the code to

    is_category('188') || (is_single() && in_category('188'))

    (and so on) that should sort that out.

  22. joshasbury
    Member
    Posted 5 years ago #

    You were right. That did the trick. Thanks!!

  23. frankganz
    Member
    Posted 5 years ago #

    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 http://codex.wordpress.org/Conditional_Tags eg

    is_category('9')
    When the archive page for Category 9 is being displayed
    ...

    in_category('5')
    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

    alan where are you? lol. I have been trying to figure this out on my own and I just can't do it!!! Please help!

  24. alanft
    Member
    Posted 5 years ago #

    not sure which bit of that you mean. if it's the 'post is in a category that is an ancestor of a parent category' then a recent solution to that was found in

    http://wordpress.org/support/topic/253564

Topic Closed

This topic has been closed to new replies.

About this Topic