php – Custom menu – Check if current page is third level sub page

Question

I’m building a custom menu* and I have run into issues with making sure the sub-menu’s is expanded when I visit pages 2-3 levels deep.

*(or rather I kinda inherited a custom menu and is now asked to alter its behavior)

The element is closed by default and the sub menuses is toggled on/off by adding the class “open” via js.

The issue is that if I visit an page 2 or 3 levels deep into the menu I want the menu to be expanded to that point, but I can’t seem to figure out how to do achieve that.
I tried using the different wp functions like get_post_parent() but so far only managed to toggle all sub-menus open two levels down not only the one I’m in when visiting a sub-menu page.

Any help or pointers in the right direction is much appreciated!

<?php

global $post;

/* Get an array of Ancestors and Parents if they exist */
$get_parent = get_post_ancestors($post->ID);

if(!empty($get_parent[2])) {
    // Level 3
    $post_parent = $get_parent[2];
} else if(!empty($get_parent[1])) {
    // Level 2
    $post_parent = $get_parent[1];
} else if(!empty($get_parent[0])) {
    // Level 1
    $post_parent = $get_parent[0];
} else {
    $post_parent = $post->ID;
}

$children = get_pages ( array( 'parent' => $post_parent ));

if( ! empty( $children ) && $children > 0 ) { ?>

    <nav class="sidebar-navigation">

        <!-- >>> Level-1 <<< -->

        <ul class="base-menu">

            <?php foreach ( $children as $child ) { 

                $children_sub_lv1 = get_pages( array ( 'parent' => $child->ID )); ?>

                <li class="menu-item">

                    <a href="<?php echo $child->guid ?>"><?php echo get_the_title( $child->ID ); ?><?php print($child_sub_lv1); ?></a>

                    <?php if( ! empty( $children_sub_lv1 ) && $children_sub_lv1 > 0 ) { ?>

                        <button class="menu-dropdown">
                            <span class="dropdown-symbol">+</span>
                        </button>

                        <!-- >>> Level-2 - If my thinking is correct i need to do a check against $child_sub_lv1 witch is not set yet at this point <<< -->
                        
                        <ul class="sub-menu <?php if ( ! empty ( $children_sub_lv1 && $post->ID === $child->ID )) { print 'open'; } ?>">

                            <?php foreach ( $children_sub_lv1 as $child_sub_lv1 ) {

                                $children_sub_lv2 = get_pages( array( 'parent' => $child_sub_lv1->ID )); ?>

                                <li class="menu-item">
                                    
                                    <a href="<?php echo $child_sub_lv1->guid ?>"><?php echo get_the_title( $child_sub_lv1->ID ); ?></a>

                                    <?php if ( ! empty( $children_sub_lv2 ) && $children_sub_lv2 > 0 ) { ?>
  
                                        <button class="menu-dropdown">
                                            <span class="dropdown-symbol">+</span>
                                        </button>

                                        <!-- >>> Level-3 - If my thinking is correct i need to do a check against $child_sub_lv2 witch is not set yet at this point <<< -->

                                        <ul class="sub-menu <?php if ( $post->ID === $child_sub_lv1->ID OR $post->ID === $child_sub_lv2->ID ) { print 'open'; } ?>">

                                            <?php foreach ( $children_sub_lv2 as $child_sub_lv2 ) { ?>

                                                <li class="menu-item">
                                                    <a href="<?php echo $child_sub_lv2->guid ?>"><?php echo get_the_title($child_sub_lv2->ID); ?></a>   
                                                </li>

                                            <?php } ?>
                                        </ul> 
                                    <?php } ?>
                                </li>
                            <?php } ?>
                        </ul>
                    <?php } ?>
                </li>
            <?php } ?>
        </ul>
    </nav>
<?php } ?>

0
nicklas bryntesson 4 days 2021-06-08T08:06:52-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse