WordPress.org

Ready to get started?Download WordPress

Forums

Show content from parent page on children (11 posts)

  1. felipedario
    Member
    Posted 2 years ago #

    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!

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  3. felipedario
    Member
    Posted 2 years ago #

    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.

  4. hotplugin
    Member
    Posted 2 years ago #

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

  5. esmi
    Forum Moderator
    Posted 2 years ago #

    Is this within the Loop?

  6. felipedario
    Member
    Posted 2 years ago #

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

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    Inside the Loop, try something like:

    foreach (($ancestors as $page_id ) :
    if( $post -> post_parent == $page_id ) echo 'Page parent!';
    endforeach;
  8. felipedario
    Member
    Posted 2 years ago #

    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 (:

  9. esmi
    Forum Moderator
    Posted 2 years ago #

    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;
  10. felipedario
    Member
    Posted 2 years ago #

    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!

  11. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic