Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Advanced Custom Fields making it easier for my client to manage his content.

On all of my child- and parent pages i would like to have the same header image and sidebar information.

Allthough i wish to make "Parent Page #1" and "Parent Page #2" have different information and this is controlled with Advanced Custom Fields by the Client.

Right now the cleint goes to every parent and child page to fill out the information manually - i would like it to only be filled out on the Parent page. Making the Child pages get the information from their parent page.

Parent Page #1

  • Child page #1 - Should get Custom Field values from "Parent Page 1"
  • Child page #2 - Should get Gets Custom Field values from "Parent Page 1"
  • Child page #3 - Should get Gets Custom Field values from "Parent Page 1"

Parent Page #2

  • Child page #1 - Should get Gets Custom Field values from "Parent Page 2"
  • Child page #2 - Should get Gets Custom Field values from "Parent Page 2"
  • Child page #3 - Should get Gets Custom Field values from "Parent Page 2"

So here's my question.

On my "parent page template" I use the following to generate the image:

<img src="<?php the_field('header_image'); ?>">

So how do I, on my "Child page template" get the field "header-image" from the parent page of the child page?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

In your loop, you can access the id of the parent page with $post->post_parent.

You just need to pass this value as second parameter:

<img src="<?php the_field('header_image', $post->post_parent ); ?>">
share|improve this answer
add comment

What about:

<img src="<?php $parent_header_image = get_post_meta($post->post_parent, 'header_image', true); echo $parent_header_image;?>">

I don't have your setup, obviously, so can't really test this, but it seems sound. Note that this just displays your parent data in the front-end template and won't have it show up or populate fields on the child page admin screen.

share|improve this answer
add comment

By the time the template file is included, the query for the page has already run and the $post global variable has a WP_Post object in it. Here is an example dump of the $post:

WP_Post::__set_state(array(
   'ID' => 3440,
   'post_author' => '1',
   'post_date' => '2013-08-19 13:06:04',
   'post_date_gmt' => '2013-08-19 13:06:04',
   'post_content' => '',
   'post_title' => 'Models',
   'post_excerpt' => '',
   'post_status' => 'publish',
   'comment_status' => 'open',
   'ping_status' => 'closed',
   'post_password' => '',
   'post_name' => 'models',
   'to_ping' => '',
   'pinged' => '',
   'post_modified' => '2013-08-23 14:32:57',
   'post_modified_gmt' => '2013-08-23 14:32:57',
   'post_content_filtered' => '',
   'post_parent' => 3442,
   'guid' => 'http://charlesclarkson.com/?page_id=3440',
   'menu_order' => 0,
   'post_type' => 'page',
   'post_mime_type' => '',
   'comment_count' => '0',
   'filter' => 'raw',
   'format_content' => NULL,
))

As you can see from the listing, there is a property in this object named post_parent. To access it, use $post->post_parent. Top level pages have a post parent set to '0'.

According to the ACF docs for the_field(), there is a second parameter for passing a post id to the function.

Assuming that the code in your page template isn't screwing around with $post:

<img src="<?php the_child_or_parent_image_src(); ?>">

<?php
function the_child_or_parent_image_src() {
    global $post;

    if ( $post->post_parent )
        the_field( 'header_image',  $post->post_parent );

    the_field( 'header_image' );
}
?>

(Note: Code is not well tested.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.