• I have a main nav menu that had 5 main parent pages listed in it, and it’s done with CSS and sprites, so there is a normal, over and active states that CSS switches between.

    Per the Codex I found and have used this if_page tag to add the an active class to the button so it will show if a visitor is on the parent page or the first level of sub papes:

    <?php if ( is_page('about') || $post->post_parent == '2' ) { echo " class=\"active\""; } ?>

    But when I add a Sub-Sub Page, meaning a of the 5 main parent pages would be a grandparent page, and navigate to the Sub-Sub Page I lose the active class. Is there something I can add or change do if I’m on in page below just a Sub Page it will still dynamically create my active class?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Erik

    (@buckycat7)

    Nothing on this?

    I struggled with this same thing. Luckily, I was just able to solve it since it was hard to find solutions online. This post helped me immensely…

    What I did was use his breadcrumb functions and put them in my functions.php

    function get_parent_id ( $child = 0 ) {
            global $wpdb;
            // Make sure there is a child ID to process
            if ( $child > 0 ) {
                    $result = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = $child");
            } else {
                    // ... or set a zero result.
                    $result = 0;
            }
            //
            return $result;
    }
    
    function get_ancestor_ids ( $child = 0, $inclusive=true, $topdown=true ) {
            if ( $child && $inclusive ) $ancestors[] = $child;
            while ($parent = get_parent_id ( $child ) ) {
                    $ancestors[] = $parent;
                    $child = $parent;
            }
            //      If there are ancestors, test for resorting, and apply
            if ($ancestors && $topdown) krsort($ancestors);
            if ( !$ancestors ) $ancestors[] = 0;
            //
            return $ancestors;
     }

    Then in my header I checked to see if the pages top-most ancestor matched my pages ID. So first I added this to header.php:

    <?php
    $ancestors = get_ancestor_ids($post->ID, false);//call to function in functions.php
    $grandparent = $ancestors[1]; // find the grandparent
    ?>

    Then on my link to a page my code would look like this:
    <?php if ( is_page('about') || $grandparent == '2' || $post->post_parent == '2' ) { echo " class=\"active\""; } ?>

    I imagine you don’t need to also have the $post->post_parent in there since the grandparent would just be the parent when you’re only one level deep…

    Hope this helps!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sub Sub Pages – Showing Main Parent Dynamically’ is closed to new replies.