WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
[resolved] Is there a "is_subpage" alternative? (19 posts)

  1. Mark Stevens
    Member
    Posted 2 years ago #

    I know there is no is_subpage syntax for widget logic, (that would be great if there was) but here is what I am looking to do.

    I wish to place a widget in the (sidebar, footer or wherever I need it).
    For this example, lets say I want to place a footer widget on the page >> http://addictiondiscussion.com/addiction-treatment-centers/california/stanislaus-county/
    I also want that same widget to show up in the same spot on the page >> http://addictiondiscussion.com/addiction-treatment-centers/california/orange-county/
    And on the page >> http://addictiondiscussion.com/addiction-treatment-centers/california/santa-cruz-county/ . They are all sub-pages of >> http://addictiondiscussion.com/addiction-treatment-centers/california/.

    I don't want to have to add all the individual page id's to the "is_page code" in widget_logic.
    Besides, when each county page fills up and needs to have a (page-2), the individual page id's will become void. Does this make sense?

    There are over 3000 counties in the US and I don't want to have 3000 different is_page codes to write.

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

  2. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    one of the sample codes on http://wordpress.org/extend/plugins/widget-logic/other_notes/ is

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

    if you can get the page ID number of 'california' that might do you.

  3. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    the next sample, adapted would work for DIRECT CHILDREN as opposed to all descendants.

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

  4. Mark Stevens
    Member
    Posted 2 years ago #

    Thanks Alan, I need to know exactly where to put that code in relation to the widget showing up on those pages.
    I registered 2 new sidebars/widget areas in functions.php. I also added them to sidebar-bottom.php so they work correctly. I have added text widgets to the new sidebars and I need each of them to show up on the pages discussed earlier.
    I am using Genesis Framework - News Child theme. The only editing capabilities I have are for home.php, functions.php & sidebar-bottom.php

    P.S. This is the shortlink location of "california" >> http://addictiondiscussion.com/?p=1153

    Is 1153 the correct "page ID" or "post ID"?

  5. Mark Stevens
    Member
    Posted 2 years ago #

    Alan, lets take this a step further and lets make the "text widget" show up only in the "Footer 3" (bottom right corner) of the pages. That is really the main objective.

  6. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    yes 1153 is the page ID in that instance.

    sorry, i'm not too sure what you are asking in that last question. you set your widgets up in each sidebar, and then you set the widget logic in that widget as per the screenshot

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

  7. Mark Stevens
    Member
    Posted 2 years ago #

    Yes, I get that, but do I put the tag >> global $post; return ($post->post_parent=="1153"); right in the widget_logic box at the bottom of the widget?

    Or would I place the >> global $post; return ($post->post_parent=="1153"); tag in the function.php file?

  8. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    in the box at the bottom of the widget where it says "Widget logic"

  9. Mark Stevens
    Member
    Posted 2 years ago #

    Thank you Alan, I really appreciate your answer. I have found posts that place the code in the functions.php, that's why i asked.

  10. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    Glad to help.

    if you have complex code it's a good idea to bundle it up in a special function, put it in functions.php and then call that from the Widget Logic…

    eg in function.php
    function my_is_direct_child_of($id)
    { global $post;
    return ($post->post_parent==$id);
    }

    in widget logic: my_is_direct_child_of(1153)

  11. aerozg
    Member
    Posted 2 years ago #

    Doesn't work for me, i just can't get it to work. I have 3 levels of navigation, 1st level is the main navigation level, 2nd level there are two pages each with their own 3rd level subpages. I want to show the Facebook comments plugin on the 1st level (main nav) page including all of her descendants, no matter the depth. None of the suggestions work. I've tried different tricks from around the web, but i was not able to achieve this. I would like to avoid modifying the functions.php file if possible.

  12. aerozg
    Member
    Posted 2 years ago #

    I thought this would work, but it doesn't:

    - return’s TRUE for all sub-pages of Parent pages ID’s 87, 342, 429, the page ID 87 being the top level main navigation page with no parent, and the page IDs 342 and 429 being child pages of page ID 87, (also having multiple subpages of their own):

    global $post; $ancestors = get_post_ancestors($post); $show_widgets_for = array(87, 342, 429); $show_widgets = FALSE; foreach ($show_widgets_for as $child) { if (in_array($child, $ancestors)) { $show_widgets = TRUE; break; } } return $show_widgets;

    It only shows the widget on the first child of the page ID 87, that being the page ID 342, and on the first child of the page ID 342, being ### ..

    Does not show the widget on all children, or descendants.

  13. aerozg
    Member
    Posted 2 years ago #

    Oh man.

    I have a theme that has custom widget groups that are hidden by default, that's why i don't see the changes.

    Yup. I need to get some sleep.

  14. David Radovanovic
    Member
    Posted 2 years ago #

    I'm having trouble with understand conditional statements. I'd like say; if NOT home page, NOT page 86 and NOT 86 subpages. I've tried this without luck:

    !is_home() || !is_page( '86' ) || '86' == $post->post_parent

    Thanks for any direction. Wouldn't it be great if there were a wordpress conditional statement builder tool?

  15. David Radovanovic
    Member
    Posted 2 years ago #

    I tried adding this function to functions.php:

    function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
            global $post;         // load details about this page
            if(is_page()&&($post->post_parent==$pid||is_page($pid)))
                   return true;   // we're at the page or at a sub page
            else
                   return false;  // we're elsewhere
    };

    and then this to widget:

    !is_home() || !is_page( '86' ) || !is_tree('86')

    No cigar.

  16. David Radovanovic
    Member
    Posted 2 years ago #

    I've also tried placing this in functions.php:

    function is_child($parent) {
      global $post;
      return $post->post_parent == $parent;
    }

    and this in widget:

    !is_home || !is_page( 86 ) || !is_child( 86)

    no good either

  17. David Radovanovic
    Member
    Posted 2 years ago #

    with && instead of || it works

  18. aljuk
    Member
    Posted 2 years ago #

    Why has this thread been marked resolved?

    It clearly isn't.

  19. esmi
    Forum Moderator
    Posted 2 years ago #

    Presumably, the original poster resolved their issue. Try posting your own topic.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.