acf icon indicating copy to clipboard operation
acf copied to clipboard

ACF Options page

Open omzy opened this issue 7 years ago • 8 comments

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:

image

How do I get the actual images?

omzy avatar Aug 07 '17 16:08 omzy

@omzy83 no no, for ACF stuff only use $post->acf->gallery('field') not Option::get(). You should receive a collection of images ;-)

jgrossi avatar Aug 07 '17 18:08 jgrossi

@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.

omzy avatar Aug 07 '17 20:08 omzy

@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...

jgrossi avatar Aug 07 '17 20:08 jgrossi

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;
    }
}

omzy avatar Aug 07 '17 20:08 omzy

@omzy83 cool, but maybe we can think in a general method to handle this.

jgrossi avatar Aug 30 '17 21:08 jgrossi

@jgrossi hey has this moved forwards at all? would really like to use this in a active project!

OwenMelbz avatar Jan 27 '18 12:01 OwenMelbz

@OwenMelbz not actually, maybe someone can create a PR for that?

jgrossi avatar Feb 09 '18 18:02 jgrossi

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.

tbruckmaier avatar Feb 12 '18 14:02 tbruckmaier