Roots Discourse

Controllers + CPT + ACF

Hey,

Briefly, I am using

Soberwp/controller 2.1.1
Brain/hierarchy 2.4

As a dependencies.

Got a custom post type named ‚project’

Created archive template based on hierarchies:

* archive-project.blade.php

* single-project.blade.php

Trying to put ACF fields into Archive template using Controllers which is named

* ArchiveProject.php

Inside ArchiveProject.php I have

namespace App\Controllers;
use Sober\Controller\Controller;
class ArchiveProject extends Controller
{
protected $acf = true;
};

And still on archive pages I’m getting undefined variables for ACF fields. Controller is working with the main App.php anyway, and the fields are working correctly on homepage. What I am doing wrong? Read all the possible topics here about this problem and nothing was helpful… Probably making something stupid :slight_smile:

What ACF fields are you trying to access on the archive? Are they ACF fields from the posts that are shown on the archive? I don’t believe the ACF feature in Controller works that way; IIRC it queries the “page” that you’re on for all of its ACF fields, and then returns them—but archives don’t have a “page” to call their own, so they have no ACF fields to return.

You are actually right, it looks like it’s working with SingleProject controller on single-project view. So how I can show ACF fields on custom post type archive pages? :thinking:

What ACF fields are you trying to show on the archive pages? How do you want to display them in your template? Where are they coming from?

I usually try to keep all of my data processing in my controllers and then pass to my blades only what they need. For archive pages, you can loop through all the posts in your controller, get ACF fields, modify them, etc, and then compose a new array that you pass to your archive to iterate over. Example: The right way to run multiple WordPress loops

Text Fields, Range Fields and Checkbox fields. Full list https://i.imgur.com/2A6gHjz.jpg
They are coming from single custom post type.

Not sure about the #2nd question, “How do you want to display them”. I’m retrieving the ACF fields like this , if you are asking about this…
{!! $project_profession !!}

I’ll read carefully the post and try your solution.
Thanks

This topic was automatically closed after 42 days. New replies are no longer allowed.