WordPress custom field for multiple pages

Tags: , ,



I am using ACF pro for creating custom field for my wordpress website. I have a custom field in home page which I want to use in another three pages. Code I have given below.

<div class="fm-link-block__grid">
            <div class="row">
            <?php if( have_rows('home-section-modeling') ): ?>
            <?php while( have_rows('home-section-modeling') ): the_row(); 
              $home_section_modelling_image = get_sub_field('home-modeling-image');
              $home_section_modelling_description = get_sub_field('home-modeling-description');
              $home_section_modelling_title = get_sub_field('home-modeling-title');
            ?>

                <div class="col-sm-4">
                    <div class="v-align-container">
                        <div class="v-align-grid">
                            <?php if( $home_section_modelling_description ): ?>
                                <p><?php echo $home_section_modelling_description ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <div class="col-sm-8">
                    <div class="fm-link-block-image">
                        <a href="#">
                            <?php if( $home_section_modelling_image ): ?>
                            <img alt="modelling-image" src="<?php echo $home_section_modelling_image['url'];  ?>">
                            <?php endif; ?>
                            <h2>
                                <?php if( $home_section_modelling_title ): ?>
                                <span><?php echo $home_section_modelling_title ?></span>
                                <?php endif?>
                                <i>
                                    <img alt="arrow icon"  src="<?php echo get_stylesheet_directory_uri(); ?>/images/link-arrow.svg">
                                </i>
                            </h2>
                        </a>
                    </div>
                </div>
                <?php endwhile; ?>
                <?php endif; ?>

            </div>
            <div class="fm-link-block-head"> MODELLING </div>
        </div>

Answer

In order to reference fields from another page, you need to include the post ID. This is similar to how you go about including fields from an Options page.

Example:

<p><?php the_field('field_name', 123); ?></p> Where 123 is the post ID.

For your example..

 <?php if( have_rows('home-section-modeling', 123) ): ?>
            <?php while( have_rows('home-section-modeling', 123) ): the_row(); 
              $home_section_modelling_image = get_sub_field('home-modeling-image');
              $home_section_modelling_description = get_sub_field('home-modeling-description');
              $home_section_modelling_title = get_sub_field('home-modeling-title');
            ?>

See https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/ as a reference to this answer.



Source: stackoverflow