Home › Forums › General Issues › Inherit field content from parent page? › Reply To: Inherit field content from parent page?
<?php
if ( !function_exists( 'get_inherited_field' ) ) {
function get_inherited_field( $field ) {
$value = get_field( $field );
if ( !$value )
$value = get_field( $field, wp_get_post_parent_id( get_the_ID() ) );
return $value;
}
function the_inherited_field( $field ) {
echo get_inherited_field( $field );
}
function has_inherited_sub_field( $field ) {
return ( has_sub_field( $field ) or has_sub_field( $field, wp_get_post_parent_id( get_the_ID() ) ) );
}
function the_inherited_sub_field( $field ) {
$value = get_sub_field( $field );
if ( !$value )
$value = get_sub_field( $field, wp_get_post_parent_id( get_the_ID() ) );
echo $value;
}
}
?>
<?php the_inherited_field('field-name'); ?>
<?php while(has_inherited_sub_field('repeater-field-name')): ?>
<?php the_inherited_sub_field('repeater-field-name'); ?>
<?php endwhile; ?>
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.