I created a hierarchical custom post type called “films.” It is hierarchical so it can have children elements.
I am using a conditional statement in my single-films.php to show different content for the parent page from the children pages like so:
<?php while(have_posts()) : the_post(); ?>
<?php if( $post->post_parent != 0 ) {
echo "Child Page";
} else {
echo "Film Page";
} ?>
<?php endwhile; ?>
In the condition that the page is a child, I want to use the values entered on the parents page, not the child page. Because the loop has already been created, I need to figure out a way to pull content from the parent page JUST on the children pages. I need it to pull any content I wish (custom field, title, featured image, etc) from it’s parent. Any idea how I can modify this loop to pull content from it’s parent?
WordPress has a nice get_post function, you can use it like this to get the parent’s title for example:
You can access any custom field by its key/name and the parent’s post id:
This works for sure with the Advanced Custom Fields Plugin for WordPress.