Access ACF field using given post ID via controller

First of all: I’m well aware there are multiple threads referring to ACF fields using controllers and blade templates, I read but found nothing that answers my question, feel free to merge this into whichever thread seems relevant (a little out of my depth here understanding something!).

I like the idea of using this: https://github.com/soberwp/controller#advanced-custom-fields-module to do something like this:

protected $acf = true;

In a controller, and then to be able to access my ACF fields by variable name, I assume by using something like {{ $fieldname }}

My question is, what would that make the blade template equivalent for e.g. get_field('fieldname', $post_id) when trying to pull in a variable from a different page? or something like get_field('twitterUser', 'option') when pulling from an ACF options page?

Apologies if is covering old ground, I’m honestly struggling to find any documentation on it.

EDIT: On a related side note, what if I have an ACF field with a name like ‘field-name’, how do we handle the hyphen - {{ $field-name }} isn’t a valid PHP variable!

Thanks.

I think you’d still have to use either a static method with Controller, or just a get_field() call in your blade template, to pass the $post_id variable the way you’re trying to.

@withjacoby might have better insight though.

Any further insight into this anyone? @withjacoby

@mikesource you can call it in App.php, which is passed to all templates. The new version of Controller (tagged yesterday) get Acf option fields by default now too.

For the side note, variables are converted to snake case, so if you’re using the acf module, then it should be $field_name in Blade.

So how do I echo the value of an option field in the blade template?

{{ $field_name, ‘option’ }} ??

Just {{ $field_name }}

Okay that works for ACF options (although seems like it would add potential for variable name clashes?).

Can you possibly elaborate on what exactly I would put in App.php and in my blade template for doing the equivalent of e.g. <?php echo get_field('fieldname', $post_id); ?>

I currently have:

App.php:

<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class App extends Controller
{
    protected $acf = true;
}

header.blade.php:

@php 

$post_id = '121' // = some page ID

the_field('fieldname', $post_id); 

@endphp

My orig question was is there any way to do that without using the ACF functions themselves?

Thanks.

a static function in App.php would work here, just pass the $post_id in.

@withjacoby Have I misunderstood the ACF Module in controller?

Playing with my code more I have set: protected $acf = true; in my App.php Controller (as above), and when I put @debug in my blade template I can see my view has all of my ACF options page fields as variables, but none of the other ACF fields.

I have a number of ACF Fields which are available to my view and can be accessed with php code like <?php echo get_field('header-h2'); ?> (no page id this time!) in my blade template, but protected $acf = true; does nothing with these with these. They don’t show up in @debug.

Is this not the intended functionality?

Controller has an useful Advanced Custom Fields helper module to automate passing on fields.

The automated fields will use the variable names from Advanced Custom Fields and pass them onto the view. Controller also passes on options values by default.

@mikesource please use 2.0.1 for now and not dev-master.

Ahhh so 2.0.2 currently adds the ACF options to ‘Data’ but loses the other fields? Is that just a bug?

With 2.0.1 I now don’t get the ACF options? Sorry for the confusion! I was just going off the docs and other threads on here and not studying your code.

That’s correct. Yea, just a bug so did not want to tag. I’ll tag 2.0.2 this afternoon if I’m able.

1 Like