Support

Account

Home Forums General Issues Inherit field content from parent page?

Solved

Inherit field content from parent page?

  • Is there a way that a child page can inherit custom field content from its parent?

    Specifically, I’m building a site that deals with many different wildlife projects. Each project is a unique section on the site made up of a main parent page and a number of child pages. All of the pages for a given project…parent page and children…will share the same sidebar content. Is there a way to populate the sidebar custom fields on just the parent page, and then have the child pages inherit that content as well?

    I know I can set up an acf options page for each project and just do it that way, but it will be much more intuitive for my client if the sidebar content can be defined on the project parent page and then just carried down to its children.

    Thanks in advance for any advice.

  • I have been trying to figure out the same thing. I don’t want to have to add a custom field over and over for custom posts that are children of a page template.

  • <?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; ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Inherit field content from parent page?’ is closed to new replies.