Home › Forums › General Issues › gettimg custom field values from parent and grandparent page?
i have a page structure like this:
– food
– – vegetables
– – – tomatos
on the “food” page i have a custom field called “country”. now i want that the 2 cild pages get the values from this field automatically. i know i could do this with the ID (http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-another-page/), but this is not what i need.
(sorry i meant “getting custom field … can’t edit the headline)
You could use the current page’s ‘post_parent’ field as the argument for get_ancestors() to ascend the page hierarchy:
global $post;
$parent = $post->post_parent;
$grandparents = get_ancestors( $parent, 'page');
$country = get_field( 'country', $grandparents[0] );
Haven’t tested this but should work
thank you very much, now this works:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'country', true);
?>
<?php $country = get_post_meta($post->post_parent, 'country', true);
echo $country;
?>
<?php
global $post;
$parent = $post->post_parent;
$grandparents = get_ancestors( $parent, 'page');
$country = get_field( 'country', $grandparents[0] );
echo $country;
?>
Hi,
i would like to reopen /reask here.
thanks herrfischer, youtr code works well.
but on one point i have difficulties and tried different things, but cant get it to work.
i have a image field, and on the grandparentspage it displays fine, but on the page above it returns the id and not the url. the same with a link field.
should be somethins easy, but i got lost.
thanks a lot!
hehe
I have tried the above but I am not having much success.
I am using a repeater field but want the same results (display parent values on a sub page).
The code I am using on the parent page is:
<?php
$rows = get_field('currentShows');
if($rows)
{
foreach($rows as $row)
{ ?>
<li>
<a href="<?php echo $row['currentLink']; ?>">
<?php echo $row['currentLinkText']; ?>
</a>
</li>
<?php
} //end for
}// endif
else {
echo 'Coming soon';
}
?>
Any help much appreciated.
Thanks
this way i get the title of the parent page, maybe the code helps you:
<?php if(is_page()):
$toppage=array_reverse(get_post_ancestors($post->ID));
if($toppage[1]){
echo '<h1 class="site-title">'.get_the_title($toppage[1]).'</h1>';
}
endif;
?>
Thank you, I have used the following to get the required results:
<?php if(get_field('currentShows', $post->post_parent)) { ?>
<?php while(has_sub_field('currentShows', $post->post_parent)): ?>
<li><a href="<?php the_sub_field('currentLink'); ?>"><?php the_sub_field('currentLinkText'); ?></a></li>
<?php endwhile; ?>
<?php }
else {
echo 'Coming soon';
} ?>
Thanks
The topic ‘gettimg custom field values from parent and grandparent page?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.