WordPress.org

Forums

Show child page when clicking parent link if parent have children (7 posts)

  1. femtontre
    Member
    Posted 7 years ago #

    Well, as the headline says, I'm using wp_list_pages to show my pages. Some of my pages have subpages, children, and when I click the parent link I want to show the content of the first child to that parent. How do I code this?

  2. kannagi
    Member
    Posted 7 years ago #

    I'm looking for this function as well. I searched the net like crazy and came apon this page: http://www.tom-carden.co.uk/2008/02/08/artnano-wordpress/

    He has a section of code further down the page labelled "Forwarding section pages to their first sub-page:" I've tried implementing it but I must be doing something wrong. Unless the code just doesn't work.

    If you have luck please post your solution :)

    thanks!

  3. wildpawtrax
    Member
    Posted 7 years ago #

    I looked for this all over. i was even using a redirect php script for a while but it is intended for using your main page links for linking to external links.

    Here is the way to redirect to a sub page.
    using the code in the link above, im just elaborating on the steps.

    1) Create a new template file and paste the following code into it.

    <?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));
      }
    }
    ?>

    2) save and upload your new template into your theme directory.

    3) Edit your Parent page and apply the "Page Template".

    Note: adding the page template is located down towards the bottom of the page. i also found it helpful to put a note in the body of the page form so that whoever edits it later wont be confused when they dont see any content.

    hope this saves somebody some time!

  4. artflutter
    Member
    Posted 6 years ago #

    Thanks wildpawtrax, I had setup a redirect using the redirection plugin but this solution is infinitely better.

  5. desmondo
    Member
    Posted 6 years ago #

    Works a treat, thanks for this!

  6. desmondo
    Member
    Posted 6 years ago #

    Hi all,

    Just one variation on this I'm trying to sort out, would it be possible to redirect to the latest page according to the timestamp and not the ID?

    I'm not good at PHP!

    Thanks

  7. pete.kyle
    Member
    Posted 6 years ago #

    Hi, that code works really well, but is it possible to redirect to first child but if there are no children then stay on the current page?

    Currently I get an infinite loop if the page has no children.

    Thanks in advance,

    Peter

Topic Closed

This topic has been closed to new replies.

About this Topic