ACF Pro Repeater Fields w/ Sage 9


#1

Hi all!

Before I ask my question, I just want to give a huge thanks to everyone on this forum. I’m brand new to WP theme dev and working with Sage, and have been asking frequent questions. Everyone here has been more than helpful, and it is very much appreciated!

That being said, I’m in need of a little more guidance. Recently, I’ve started playing with ACF Pro and the repeater fields. The repeater field group has its own ID (in my case, portfolio_work), and then the subfields have their own name/IDs (portfolio_image, portfolio_description, etc…). How would I go about pulling that data using Controllers?

Thanks again in advance.


Custom post type archive on a custom page template
Advanced Custom Fields Repeater Field, Controllers
#2

There are a lot of ways to answer this question because it’s not really a right or wrong question, it’s more of a stylistic one.

That said, I think the Sageiest way to deal with repeaters and controllers is as follows. This example is adapted from a recent site that needed to list projects:

app/controllers/App.php

...

    /**
     * Populate and return the Projects template(s)
     */
    public function projects() {

      // Initialize the return variable
      $return = '';

      // Get the appropriate projects
      $projects = get_field('projects');

      if(is_array($projects)) {

        foreach( $projects as $p ) {

          $return .= \App\template('partials.projects.project-item', [
            'title' => $p['title'],
            'content' => $p['content'],
            'image' => wp_get_attachment_image( $p['image'], 'medium' ),
            ]
          );

        }
      }

      // Always return
      return $return;
    }

...

resources/views/projects/project-item.blade.php

<li>
  <header>
    <h3>{!! $title !!}</h3>
  </header>
  <figure>
    {!! $image !!}
  </figure>
  <article>
    {!! $content !!}
  </article>
</li>

And then just put this wherever you want them to appear:

<ul>
  {!! $projects !!}
</ul>

The reason I like this method is that’s it takes every bit of logic out of the templates and puts it into the controller. No ifs, no foreaches, justnice, clean templates.


#3

Worked like a charm. Thank you so much!


#4

Just wanted to add a possible alternative:

app/controllers/App.php

public function projects()
{
   return array_map(function($project) {
      return [
         'title' => $project['title'] ?? null,
         'content' => $project['content'] ?? null,
         'image' => isset($project['title']) ?  wp_get_attachment_image( $project['image'], 'medium' ) : null,
      ];
   }, get_field('projects') ?? []);
}

resources/views/projects/project-item.blade.php

<header>
  <h3>{!! $title !!}</h3>
</header>
<figure>
   {!! $image !!}
</figure>
<article>
   {!! $content !!}
</article>

Wherever you want them to appear

<ul>
   @foreach($projects as $project)
      <li> @include('projects.project-item', $project) </li>
   @endforeach
</ul>

This isn’t any more “correct” than @MWDelaney’s version: It’s just preference. I like to have controllers return only data, and have views communicate structure. Using @include()instead of a compiled string makes it a little clearer to someone reading the Blade what this element is doing, and keeping the <li> out of the partial makes the partial a bit more flexible. Also array_map() is :ok_hand::ok_hand::ok_hand: and has changed my life.


#5

This is good. My way definitely runs the risk of being included without the <ul>.

It’s all personal preference.

I think my way is absolutely preferable if you’re passing instance-specific data, rather than page-specific data, and using a static function. That’s what I’m doing most often and I fell into the habit of doing @foreaches this way from that.


#6

Hi guys, thank you so much for helping us to understand how Sage 9 works.

I would like to know, what is the best way to use this example with custom post types and nested repeaters.

I have a custom post type called “books” with an ACF repeater with 3 subfields called “year”, “author” and “shops” (“shops”, is the ACF nested repeater with two fields (“name” and “url”)).

I have several days, trying and I only get one Notice: Undefined variable. :disappointed_relieved:
Can you help me, please?

PS: What a book they recommend for a newbie like me?
Thanks.


#7

Well first of all, are “shops” items ever repeated among books? Do two or more books have the same shop attached to them? If so, I would use a custom post type (and a relationship field), or custom taxonomy (and a taxonomy field) depending on your needs.

That way you can enter the names and URLs once and reuse them wherever you need.

To diagnose the error you’re receiving we’d need to see your code. What is in your template? What’s is in your controller?


#8

Thank you for answering me @MWDelaney .

The links of the stores are not repeated, the name corresponds to the name of the store and the link corresponds to the link of the book for your purchase.

For example
*Amazon
*https://www.amazon.com/Los-Logos-8-Gestalten/dp/3899556941/

Thanks for helping me with my code, I was able to remove the undefined variable message, but I still can’t get the ACF repeater works. Can you help me?

Field Group

Year = Text Field (year)
Author = Text Field (author)
Stores = Repeater (stores)
    -- Name = Text Field (name)
    -- Url = Url Field (url)

app/controllers/front-page.php

public function books()
{
    $book_items = get_posts([
        'post_type' => 'book',
        'posts_per_page'=>'10',
    ]);

    return array_map(function ($post) {
        return [

            // Title, Content and Image
            'title' => apply_filters('the_title', $post->post_title),
            'content' => apply_filters('the_content', $post->post_content),
            'thumbnail' => get_the_post_thumbnail($post->ID, 'large'),

            // ACF Fields
            'acf_year' => get_field('year', $post),
            'acf_author' => get_field('author', $post),

            // Repeater Store Name and Link

        ];
    }, $book_items);
}

resources/views/partials/content-front-page.blade.php

@debug
@if($books)
    @foreach($books as $book_item)
        {!! $book_item['title'] !!}
        {!! $book_item['acf_year'] !!}
        {!! $book_item['acf_author'] !!}
        {!! $book_item['content'] !!}
        {!! $book_item['thumbnail'] !!}

        // ACF Repeater

    @endforeach
    @else
        Not found
    @endif

Thanks Man!
Have a nice day.


#9

It looks like you have @debug set at the top, what does it output? What does or doesn’t work when you use this code?


#10

You could try temporarily changing the @foreach content to @dump($book_item) so you can see if you’re getting all the requested data back properly.