tags:

views:

42

answers:

3

I have a main page that has a children item. Now my question is, is it possible to have the Main page link directly to the children item in it? E.g.

-Main Page(links to Page 2)

---Page 2

Here is my code:

<div id="MainNav">
<ul>
<?php wp_list_pages('exclude=3&sort_column=menu_order&title_li=&depth=1'); ?>
</ul>
</div>
<div id="leftCol">
<?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) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } else { ?>
<?php } ?>
</div>
+1  A: 

At first glance, it looks like your approach should work. Can't really say more without more info about your problem.

As an alternative, I did a similar thing on the blog I maintain, but needed a bit more control over the output. So I ended up rolling my own function. Here's how I did it:

// Generate the subnav for a page
// Returns a list of links to child pages
// if there are no children, returns a blank list
function get_subnav($page_id)
{
     $current_page = get_page($page_id);
     if ($current_page->ancestors) {
       $child = get_page($page_id);
       $ancestor_id = $child->ancestors[0];
       $page = get_page($ancestor_id);
     } else {
       $page = get_page($page_id);
     }

     $children = get_children('post_parent=' . 
            $page->ID . '&post_type=page&post_status=publish&order=ASC&orderby=menu_order');

     if ($children) {
       $html = '';
       foreach ($children as $child) {
       $html .= "<li><a href='" . get_page_link($page->ID) . "'>" . get_post_meta($page->ID, 'nav_name', true) . "</a></li>\n";
       }
     } else {
       return false;
}


return $html;

}

I call it like this: get_subnav($post->ID)

Note that it won't climb the entire tree of child pages. If you're calling it from a child page, it will construct the navigation using its first ancestor as the root node.

Bryan M.
Thank you. There is no problem I just wanted to know if it is possible to link a Main Page to the first children. So let's say you have a main nav "About us" and inside you have subnavs under About us e.g. "who we, team, news". I want the "About us" to link to "who we" not to a page that says About us.
kwek-kwek
A: 

I don't understand.

The code you provided works with no errors thrown (not even in debug mode). Are you asking if that works? If so, yes it does. If it's not working for you, please tell us your specs and where in your theme you're adding this.

Also, instead of $post->post_parent, you should probably use $wp_query->post->post_parent. It's safer, if someone's been using custom queries and calling them $post in the loop.

John P Bloch
A: 

I've done this before by creating a Page template that automatically redirects to the first child. Perhaps you could use that. Add a file in your template directory called tofirstchild.php and put this in it:

<?php

/*
Template Name: Redirect To First Child
*/

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        $pagekids = get_pages( "post_parent=" . $post->ID . "&sort_column=menu_order" );
        $firstchild = $pagekids[0];
        wp_redirect( get_permalink( $firstchild->ID ) );
    }
}

?>

Then in the Admin area, select "Redirect to First Child" template for the "About Us" page.

Strider
If you saw this right after I posted it, there was an error in the code. It's corrected now.
Strider