• Hello. I guess this may be possible, but haven’t found anything on Google or Stack Overflow: can one put content from a parent page (say, the title and custom fields data) on it’s children?

    I would like to do it automatically for each page group (parent and children), without needing to use different page templates for each group.

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Yes – it’s possible. You’d need to use get_ancestors() to grab the current page’s parent. If a parent id is returned, use that in a secondary loop to grab the parent’s content as needed.

    Thread Starter felipedario

    (@felipedario)

    Thank you very much, esmi!

    So, using

    <?php
    $ancestors =  get_ancestors($post->ID, 'page' );
    echo "<pre>"; print_r($ancestors); echo "</pre>";
    ?>

    I got

    Array
    (
        [0] => 25
        [1] => 23
    )

    I just want to show the direct parent, “25”, not the grandparent, “23”. Do you know how to get just the direct parent?

    Also, could you show a pratical way to make the loop work?

    Thank you again.

    you could use this:
    echo current($arr);
    or
    echo reset($arr);

    Is this within the Loop?

    Thread Starter felipedario

    (@felipedario)

    I’m using outside the Loop right now, but I can move it within.

    Inside the Loop, try something like:

    foreach (($ancestors as $page_id ) :
    if( $post -> post_parent == $page_id ) echo 'Page parent!';
    endforeach;
    Thread Starter felipedario

    (@felipedario)

    WordPress is getting an error with line 6, “unexpected T_AS”.

    <?php
    $ancestors =  get_ancestors($post->ID, 'page' );
    echo "<pre>"; print_r($ancestors); echo "</pre>";
    ?>
    
    <?php
    foreach (($ancestors as $page_id ) :
    if( $post -> post_parent == $page_id ) echo 'Page parent!';
    endforeach;
    ?>

    This prints the direct parent id, but I still don’t see how to get it’s the_title and post_meta, for example:

    <?php
    echo current($ancestors);
    ?>

    Thanks for helping this noob (:

    My bad – I added an extra opening bracket. It should be

    <?php
    foreach ($ancestors as $page_id )  :
    if( $post -> post_parent == $page_id ) echo 'Page parent!';
    endforeach;
    Thread Starter felipedario

    (@felipedario)

    Thanks, esmi.

    So I guess I’m making a little progress:

    <?php
    foreach ($ancestors as $page_id )  :
    if( $post -> post_parent == $page_id )
    {
        $parent_title = get_the_title($post->post_parent);
        $parent_link = get_permalink($post->post_parent);
    
        echo $parent_title;
        echo $parent_link;
    }
    endforeach;
    ?>

    does print the title and permalink of the parent.

    But I don’t know how to use html inside an echo. Do you have any tutorial to point or tip?

    Thanks again!

    Now that you can identify the correct parent id, you can use get_page or get_posts to grab the content for that parent page.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Show content from parent page on children’ is closed to new replies.