Support » Plugins » wp_list_pages links to first child

  • Hello all,

    I’m using wp_list_pages to list all of my pages but for my architecture I would like the parent to point to the first child pageif there is a child. Example, if there is a page called ‘for clients’ with a child page called ‘client overview’ I would like the listing of ‘for clients’ to link directly to ‘client overview’.

    My current solution is to make a page template that uses the wp-redirect as listed below. Then I just have to pick the template for all the parent pages.

    Template Name: Redirect To First Child
    if (have_posts()) {
    while (have_posts()) {
    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    $firstchild = $pagekids[0];

    The problem is that the client doesn’t like the clicking from the redirect, especially on the pages that have two children levels or more and that it’s manual. I would like to set it up and let the client add and delete pages at will.

    Is there a hook/filter I could use? Plug-in? Please advice.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Follow up with where I started… from this post.

    Your request and explanation is confusing and ideas run together.
    It took me longer than normal to decipher what you are getting at.
    You need to be more explicit. Links? Point to’s?
    Do you mean “automatic redirection?”

    I came across this problem last night and here is how I solved it. My client wanted it so if you clicked on a parent in the menu you would be linked to the first child. Not in this example the menu only shows parents:

    function gl_list_pages() {
    	$pages = wp_list_pages('echo=0&sort_column=menu_order&depth=1&title_li=');
    	$pages = explode("</li>", $pages);
    	$children = get_pages('sort_column=menu_order');
    	$childrenParent = array();
    	foreach ($children as $child){
                    //we're going to loop through all of the pages and get rid of any that aren't children.
    		if ($child->post_parent == 0) continue;
    		$childrenParent[$child->post_parent] = $child;
    	$count = 0;
    	foreach($pages as $page) {
    		foreach ($childrenParent as $child){
                            //now we're going to find the link to the parent and replace it with a link to the first child
    			if (strstr($page,'page-item-'.$child->post_parent)){
    					$newurl = 'href="'.get_permalink($child->ID).'"';
    					$page = preg_replace('/href="(.*)\/"/',$newurl,$page);
    		$pages[$count] = $page;
    	$pages = implode('</li>',$pages);
    	echo $pages;

    I’m sure someone with a better grasp of php could make this code have a few less loops in it, but this worked for my situation.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_list_pages links to first child’ is closed to new replies.