• Resolved Ricky Irvine

    (@dressedinvalue)


    I’m using one template for the front of several exhibits. From these pages I need to link directly to the first child of each exhibit, using a “Start Here” link.

    I’ve found a couple solutions to this, but none have worked, such as this one on stackoverflow:

    <?php
        if ($children = get_children('post_type=page&numberposts=1')) {
            $first_child = $children[0];
            $first_child_permalink = get_permalink($first_child->ID);
            echo '<a href="' . $first_child_permalink . '">Start Here</a>';
        }
    ?>

    It looks right. What’s wrong? Any other ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Ricky Irvine

    (@dressedinvalue)

    I’ve got part of it working. The array was written incorrectly. $children var_dumps the correct page. Now I am having trouble getting that page’s url.

    $page_id = get_the_ID();
    $children = get_children(array(
    	'post_type'   => 'page',
    	'post_parent' => $page_id,
    	'numberposts' => 1
    	));
    $first_child = $children[0];
    $first_child_permalink = get_permalink($first_child->ID);
      echo '<a href ="' . $first_child_permalink . '">Start here</a>';
    Thread Starter Ricky Irvine

    (@dressedinvalue)

    I can’t understand why $children[0] is returning null. The variable is not empty. This is the beginning of it…

    array(1) { [582]=> object(WP_Post)#124 (24) { [“ID”]=> int(582) [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2014-05-06 10:34:17” [“post_date_gmt”]=> string(19) “2014-05-06 14:34:17” [“post_content”]=> string(676) …

    How can I grab that first element in the array?

    Thread Starter Ricky Irvine

    (@dressedinvalue)

    I see. [582] in the above array is not the first element. It is the 582nd element.

    Thread Starter Ricky Irvine

    (@dressedinvalue)

    GOT IT!

    Instead of using get_children I used get_pages. (Thanks to James Fishwick!)

    $children = get_pages("child_of=".$post->ID."&sort_column=menu_order");
    $first_child = $children[0];
    $first_child_permalink = get_permalink($first_child->ID);
    echo '<a href="' . $first_child_permalink . '">Start Here</a>';
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get permalink to first child page’ is closed to new replies.