acf
acf copied to clipboard
ACF Options page
How do we retrieve values from an Options page?
https://www.advancedcustomfields.com/resources/options-page/
I added a gallery field within an options page, when I do Option::get('gallery')
it outputs an array (which is correct), however that array only contains key => value
pairs, like this:
How do I get the actual images?
@omzy83 no no, for ACF stuff only use $post->acf->gallery('field')
not Option::get()
. You should receive a collection of images ;-)
@jgrossi I think you misunderstood...
I created the gallery in the Options page because it will be used on different pages on the site. I.e the same gallery with the same images reused across different pages.
@omzy83 hum good call, I don't think we already have get_option()
covered... it's easy to implement but I think it's not working yet...
As a workaround for now, I have done this which seems to work:
<?php
namespace App\Models;
use App\Models\Post;
class Option extends \Corcel\Model\Option
{
public static function getGallery()
{
// 'options_gallery' is the name of my Options page field
$gallery_option = self::get('options_gallery');
$gallery = [];
foreach ($gallery_option as $index => $id) {
$post = Post::find($id);
$image = $post->url;
$gallery[] = $image;
}
return $gallery;
}
}
@omzy83 cool, but maybe we can think in a general method to handle this.
@jgrossi hey has this moved forwards at all? would really like to use this in a active project!
@OwenMelbz not actually, maybe someone can create a PR for that?
I am currently working on a pr for this, unfortunately it requires a bunch of changes. The more complex fields like repeater or even image need to retrieve additional data from wp_posts and wp_postmeta. The logic for this is currently in the single field classes. The acf option page stores its data in wp_options though, that's why its not working with the current approach. My approach is to move the logic for additional fields to a new layer (called "repository" now in lack of a better name), so we have a PostRepository (with the current logic from the single Field classes) and a new OptionPageRepository. The single fields do interact with the database only through their repository, so we should be fine there.