Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Getting fields from another page with ACF

Hi all so i'm using ACF and have set up a relationship field and on page A i have set up the relationship between page A & page B but the issue i'm having is that i don't know how to reference this relationship in the php code and pull the data from another page could anyone offer any insights into doing this or advice sorry if i'm not too clear i don't really use wordpress a lot.

Zac Gordon

1 Answer

Stephen O'Connor
Stephen O'Connor
22,291 Points

I have done this before with posts and custom post types, this will give you an option in the admin to pick and choose what posts to display on the page and what order to display them in. Edit the code as you see fit.

<?php

$posts = get_field('home_projects');

if( $posts ): ?>

    <?php
        $ids = get_field('home_projects', false, false);
        $args = array(
            'post_type' => 'portfolio',
            'post__in' => $ids,
            'orderby' => 'post__in',
            'posts_per_page' => 6
        );

        $the_query = new WP_Query( $args );
    ?>

    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php
            $project_size = get_field('project_image_size');
        ?>
        <div class="portfolio-item-container <?php if ($project_size == 'Big') { echo 'item w2'; } else { echo 'item'; } ?>">
            <a href="<?php the_permalink(); ?>">
                <div class="portfolio-item">
                    <div class="portfolio-item-image">
                        <?php 
                            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio_main_image' );
                            if ($image) : ?>
                                <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
                            <?php endif; ?>
                    </div>
                    <div class="portfolio-item-heading">
                        <h4><?php the_title(); ?></h4>
                        <p><?php the_field('project_categories'); ?></p>
                    </div>
                </div>
            </a>
        </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    <?php endif; ?>

<?php endif; ?>

If this is not what you are looking for you can use the code on this page to get values from other pages and display them in your theme.

Hope this helps.

Thanks so much mate i'll have to try that out later :)