WordPress.org

Ready to get started?Download WordPress

Forums

Navigation - 3rd level (4 posts)

  1. boon_
    Member
    Posted 5 years ago #

    Hey

    I've got my sub-navigation set up all follows:

    <?php if($post->post_parent)
    $children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
    $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
                	<ul>
    		    <?php echo $children; ?>
    		</ul>
    <?php } ?>

    Which is fine - if I'm on a first or second level page it displays the sub menu showing the second level pages.

    However when I switch to a third level page the menu changes to display only the third level pages. How can I tweak it so it still only shows the second level pages as before?

    Basically I want my navigation to display the second level pages ONLY all the time, even if you go to a third/fourth level page.

    Thanks

  2. pirazolone
    Member
    Posted 5 years ago #

    Any luck with this, I am using the following code and have the same problem:

    <?php
    	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="sideblock-pages">
                        <ul class="subpage-list">
                        <?php echo $children; ?>
                        </ul>
            </div>
    <?php } ?>
  3. Kevin Leary
    Member
    Posted 4 years ago #

    Try using the get_post_ancestors in place of post_parent. This approach seemed to work for me:

    <?php
    global $wp_query;
    $post = $wp_query->post;
    $ancestors = get_post_ancestors($post);
    if( empty($post->post_parent) ) {
    	$parent = $post->ID;
    } else {
    	$parent = end($ancestors);
    }
    if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) { ?>
    
    <ul id="secondary-nav">
    	<?php wp_list_pages("title_li=&child_of=$parent&depth=1" ); ?>
    </ul><!-- #secondary-nav -->
    
    <?php } ?>

    Then I used this to target the current nav states with CSS:

    #secondary-nav li a:hover,
    #secondary-nav li.current_page_item a,
    #secondary-nav li.current_page_ancestor a {
    	background:#fcb701;
    }

    I hope this helps!

  4. 2bak860
    Member
    Posted 4 years ago #

    Can you get the 4th level using a similar method?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.