• Hi i’m using wp as a basic cms for a small site mostly created using pages.
    I’m currently trying to make the sidebar change content based on site “section”.
    I’m using is_page(X) tag which works fine but i have to set up a conditional statement for every page ( if….elseif…elseif etc.), also for child pages.
    The_Question: is there any way to manipulate page branches? I’d like subpages to show the same sidebar content as their parent (parent and child make 1 “section”) without having to add them manually using another “elseif”.
    This way i won’t need to make any change if a subpage is added and the code would be tidier. I really hate wasting lines!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You could probably hack something up quickly if you are using the IDs for reference, and only have only level of subpages.


    function is_page_or_subpage($id)
    { /* this can be used instead of is_page FOR PAGE ID TESTS ONLY */
    global $wp_query;

    if (! $wp_query->is_page) {
    return false;
    }

    if (empty($id)) {
    return true;
    }

    $page_obj = $wp_query->get_queried_object();
    if ($id == $page_obj->ID) {
    return true;
    }

    $pages = get_pages();
    foreach ($pages as $page) {
    if ($page->ID == $page_obj->ID /* found the page we're on */
    && $page->post_parent == $id) /* parent matched the passed ID! */
    return true;
    }

    return false;
    }

    Thread Starter gionni

    (@gionni)

    Hey nice and sweet.
    I need to do some more testing but it seems to be working.
    Thank you SO much!

    thanks davidchait,

    i have used your function in the following code.

    <?php

    function is_page_or_subpage($id)
    { /* this can be used instead of is_page FOR PAGE ID TESTS ONLY */
    global $wp_query;

    if (! $wp_query->is_page) {
    return false;
    }

    if (empty($id)) {
    return true;
    }

    $page_obj = $wp_query->get_queried_object();
    if ($id == $page_obj->ID) {
    return true;
    }

    $pages = get_pages();
    foreach ($pages as $page) {
    if ($page->ID == $page_obj->ID /* found the page we're on */
    && $page->post_parent == $id) /* parent matched the passed ID! */
    return true;
    }

    return false;
    }

    ?>

    <!-- Pages Fruit -->
    <?php if(is_page_or_subpage(5)): ?>
    <a href='<?php echo get_settings('home');?>/fruit'>Fruit</a><br>
    <ul>
    <?php wp_list_pages('title_li=&exclude=2,3,4,6,7,8,9,10,11,12,13&sort_column=post_name&child_of=5' ); ?>
    </ul>
    <?php else : ?>
    <a href='<?php echo get_settings('home');?>/fruit'>Fruit</a><br>
    <ul>
    </ul>
    <?php endif; ?>

    <!-- Pages Animals -->
    <?php if(is_page_or_subpage(7)): ?>
    <a href='<?php echo get_settings('home');?>/animals'>Animals</a><br>
    <ul>
    <?php wp_list_pages('title_li=&exclude=2,3,4,5,6,8,9,10,11,12,13&sort_column=post_name&child_of=7' ); ?>
    </ul>
    <ul>
    </ul>
    <?php else : ?>
    <a href='<?php echo get_settings('home');?>/animals'>Animals</a><br>
    <ul>
    </ul>
    <?php endif; ?>

    How to:

    Just paste the above code into your sidebar.php

    In the above example, page “Fruit” has the ID 5.

    Add the ID in:

    1) is_page_or_subpage(5)
    2) child_of=5

    Add exclude IDs:

    Here one enters the
    exclude=2,3,4,6,7,8,9,10,11,12,13

    Notice in this example that the 5 is not in the “exclude” list.

    Add the Links:

    The links are hardcoded URLs. It would be naturally
    more elegant if these URLs could be created from the ID
    but my knowledge of the WordPress system isn’t that great.

    repeat for Pages Animals, etc.

    this works very well for me. of course the top level (parent pages) need to be
    hard coded, as in the above example however the children pages will be
    dynamically displayed. this would work fine for a simple CMS site.
    I guess this only works for a two level hierachy though.
    thought this might help someone out there.

    steven

    ps. i have added this post into a text file at:
    http://www.fluxmedia.de/images/WP_collapsable.txt
    because this post seems to clip the length on some of the code lines.

    askthefool

    (@askthefool)

    Sorry to bring this up from the acrhives, but I was wondering if there was a way to do the is_page_or_subpage() for subpage of a subpage. It works excellent when there is only one nested page, but if that nested page has another nested page it breaks.

    Please let me know.

    Took me some time to find this topic and the function listed above.. Made som changes to track back one level.. don’t know if it’s the best solution but i works for me..

    ….foreach ($pages as $page)
    {

    if ($page->ID == $page_obj->ID && $page->post_parent == $id)
    {
    return true;
    }

    // Track back..
    if ($page->ID == $page_obj->ID)
    {
    $check1 = $page->post_parent; // get current post_parent
    foreach ($pages as $page)
    {

    if ( $page->ID == $check1 && $page->post_parent == $id)
    return true;
    }
    }

    }…..

    / Andreas

    I’d also like to know if it’s possible to extend this function to include subpages of a subpage.

    Anybody?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘context aware sidebar’ is closed to new replies.