WordPress.org

Forums

[resolved] Highlighting topmost parent (8 posts)

  1. ChrisR
    Member
    Posted 10 years ago #

    Hi,

    I've got a site which has tabs ontop for every level 1 page i added in the admin of WP. I fetch the pages with wp_list_pages('title_li=&depth=1') and wordpress puts current_page_item as class for the active page nicely. This works as its supposed to but now i've come to the point where i'm adding subpages to each level 1 page and i would like the highlighting on the top to remain in place even if you are visiting a page at depth 2 or deeper.

    I know this it's possible with hardcoded menu's but thats no option since i'm generating the menu dynamically with wp_list_pages.

    Next to that i have a secondary navigation in place which displays the current subpages of the selected tab.

    This is what i'm basically asking for: it would be perfect if someone knows a way to apply the current_page_item class not only to the current page in the menu but to every parent page of the current page in the menu too.

    Any help?

  2. ChrisR
    Member
    Posted 10 years ago #

    I've worked out a plugin which i'm going to clean up a little, strip the dirty code and i'll post it here ... only drawback is the immense number of queries if you got quite a big hierarchical layout in the pages. But we'll have to live with that i'm afraid :)

  3. johanvanderwijk
    Member
    Posted 10 years ago #

    I have contacted Chris, and he kindly sent me the code he has used for his website (Thanks Chris!).

    I am going to try to implement this:


    <?php
    $highlightclass = "current_page_item";

    // highlight 'Blog' if not Page or Home
    if (is_home()) {
    $homehighlight = $highlightclass;
    } elseif (is_page("blog")) {
    $highlight = $highlightclass;
    } elseif (is_page()) {
    $highlight = "";
    } else {
    $highlight = $highlightclass;
    }

    if( is_page() ) {
    $post_name = page_parents();
    }
    rewind_posts();
    //echo "<pre>".print_r($post_name, true)."</pre>";
    ?>

    <div id="navigation">

      <li class="<?php echo $homehighlight; ?>">">Home
      <li class="<?php echo $highlight; ?>">/blog/">Blog
      <?php list_pages_highlighted('exclude=2,3&title_li=&depth=1&highlightall=1&sort_column=menu_order'); ?>

    </div>

  4. checksum
    Member
    Posted 10 years ago #

    I'm also trying to figure this out - the above code doesn't work for me.

    I get the error: Call to undefined function: page_parents()

    Did you manage to sort this out Chris or Johan?

    I've tried to put some alternative code together (re: http://codex.wordpress.org/index.php?title=Dynamic_Menu_Highlighting&redirect=nothough it ain't bloody working :()

    <div id="mainNav">
    <div>
    <ul>
    <li id="one"><a href="#">Home</a></li>
    <li id="two"><a href="#">Services</a></li>
    <li id="three"><a href="#">About</a></li>
    <li id="four"><a href="#">Contact</a></li>
    </ul>
    </div>
    </div>

    <?php
    $parent_id = $page_query->ID;
    if ($parent_id = 3) { $current = 'one'; }
    elseif ($parent_id = 5) { $current = 'two'; }
    elseif ($parent_id = 2) { $current = 'three'; }
    elseif ($parent_id = 4) { $current = 'four'; }
    ?>

    Cany anyone help?

    Cheers

  5. checksum
    Member
    Posted 10 years ago #

    Can anyone shed any light on this or am I in the wilderness?

  6. checksum
    Member
    Posted 10 years ago #

    I have done it this way with David Chait's function:

    <?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;
    }

    ?>

    <div id="mainNav">
    <div>
    <ul>
    <?php if(is_page_or_subpage(3)): ?>

    <li class="page_item"><a href="#" title="Home"><span>Home</span></a></li>
    <li class="page_item current_page_item"><a href="#" title="Services"><span>Services</span></a></li>

    <?php else : ?>

    <li class="page_item current_page_item"><a href="#" title="Home"><span>Home</span></a></li>
    <li class="page_item"><a href="#" title="Services"><span>Services</span></a></li>

    <?php endif; ?>

    </ul>
    </div>
    </div>

  7. strugglefish
    Member
    Posted 10 years ago #

    johanvanderwijk: Trying to implement the same thing you have, however i'm getting an error


    Fatal error: Call to undefined function: page_parents() in...

    Can you let us all know how you've implemented this functionality?

  8. strugglefish
    Member
    Posted 10 years ago #

    Johan kindly replied referncing the following thread:

    http://wordpress.org/support/topic/55775?replies=5

Topic Closed

This topic has been closed to new replies.

About this Topic