• Hi everyone.

    I’m trying to get a child menu working for a parent / sub-page set of pages.

    I’m using this code to call the pages which works fine to display them but the class is empty when I’m on the sub-page and I need this to be active so I can highlight it in the menu to show thats where the user is.

    if ($post->post_parent)
                $children = wp_list_pages("title_li=&child_of=" . $post->post_parent . "&echo=0");
            else
                $children = wp_list_pages("title_li=&child_of=" . $post->ID . "&echo=0");
            if ($children) {
                ?>
                <div class="row">
                    <div id="news-menu" class="span3 border-right">
                        <ul class="nav nav-tabs nav-stacked uppercase">
                            <li class="menu-heading"><h6>OVERVIEW</h6>
                            <?php echo $children; ?>

    Output to browser is:

    <li class=""><a>SYSTEM MANAGEMENT SERVICES</a>
    Any ideas why WordPress isn’t outputting the active class?

Viewing 1 replies (of 1 total)
  • Thread Starter By Paul Harrison

    (@by-paul-harrison)

    I also tried this code to get the same output with no active class set on the page I am looking at:

    $has_subpages = false;
        // Check to see if the current page has any subpages
        $children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
        if($children) {
            $has_subpages = true;
        }
        // Reseting $children
        $children = "";
        // Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)
        if(is_page() && $post->post_parent) {
            // This is a subpage
            $children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
            $children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
        } else if($has_subpages) {
            // This is a parent page that have subpages
            $children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
            $children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
        }
    ?>
    <!--Check to see if we have anything to output-->
    <?php if ($children) { ?>
         <div id="news-menu" class="span3 border-right">
    	<ul class="nav nav-tabs nav-stacked uppercase">
                <li class="menu-heading"><h6>OVERVIEW</h6>
        	    <li class="menu-heading">
                <!--modified wp_list_pages-->
    	        <?php
                        $var1 = '<a';
                        $var2 = '<span class="active"><a';
                        $var3 = '';
                        $var4 = '</span>';
                        $children = str_replace($var1, $var2, $children);
                        $children = str_replace($var3, $var4, $children);
                       echo $children;
                    ?>
                    <!--end of modified wp_list_pages-->
    
        </div><!--subNav-->
    <?php }

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

Viewing 1 replies (of 1 total)
  • The topic ‘Active li class not being added to child pages menu’ is closed to new replies.