Support

Account

Home Forums General Issues Relationship field to display featured posts

Helping

Relationship field to display featured posts

  • I have a custom post type called “Portfolio”. Each post in portfolio has several ACF fields. One example is a ‘location’ field to set the city of the property in the portfolio.

    On the homepage I have a section to display 3 featured portfolio posts. I’m using the relationship field to choose those 3 posts in a loop.

    I’m trying to figure out how to pull in the value from the ‘location’ field but having trouble.

    I’m able to pull in the standard WP title and featured image but unable to pull in the ‘location’.

    Here’s my template file code:

    <div class="flex flex-wrap m-20">
        @foreach ($featured as $f)
            <div class="relative w-full md:w-1/2">
                <div class="p-4">
                    <a href="{{ $f->permalink }}" title="{{ $f->title }}">       
                        <div class="absolute top-0 right-0 bottom-0 left-1/2 bg-secondary opacity-20 z-50 m-4 p-4">
                            <div class="absolute bottom-0 right-0 text-right text-white pr-8 pb-8">
                                <p class="m-0 text-white">{{ $f->title }}</p>
                            </div>
                        </div>     
                        {!! $f->thumbnail !!}
                    </a>
                </div>
            </div>
        @endforeach    
        <div class="w-full md:w-1/2 order-first">
            <div class="flex items-center h-full">
                <div class="flex-1">
                    <div class="w-4/5 mx-auto">
                        {!! $intro !!}
                </div>
                </div>
            </div>
        </div>
    </div>

    Here’s my controller for the relationship field:

    public function featured() {
        global $post;
        $data = [];
        $featured = get_field('featured_portfolios');
            foreach ($featured as $f) {
                $this_post = (object) array(
                    'thumbnail' => get_the_post_thumbnail($f, 'hero', array('class' => 'lozad')),
                    'permalink' => get_the_permalink($f),
                    'title' => get_the_title($f),
                );
                array_push($data, $this_post);
            }
            return $data;
        }
  • A little bumpity bump bump!

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Relationship field to display featured posts’ is closed to new replies.