WordPress.org

Ready to get started?Download WordPress

Forums

wp_list_pages links to first child (4 posts)

  1. iDRez
    Member
    Posted 5 years ago #

    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.

    <?php
    /*
    Template Name: Redirect To First Child
    */
    if (have_posts()) {
    while (have_posts()) {
    the_post();
    $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    $firstchild = $pagekids[0];
    wp_redirect(get_permalink($firstchild->ID));
    }
    }
    ?>

    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.

  2. iDRez
    Member
    Posted 5 years ago #

    Follow up with where I started... from this post.

    http://wordpress.org/support/topic/241439?replies=4

  3. mkormendy
    Member
    Posted 5 years ago #

    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?"

  4. bobby_baboon
    Member
    Posted 4 years ago #

    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;
    		$count++;
    	}
    	$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.

Topic Closed

This topic has been closed to new replies.

About this Topic