Retrieve and Display WordPress ACF Repeater Field Data
ACF, PHP, WordPress
The Advanced Custom Fields Pro WordPress plugin has a repeater field function that allows content editors to add multiple field values.
For example, the site editor might need the ability to select files that will be available for download. Setting up an ACF repeater field allows as many files as necessary to be associated with the post being edited.
The ACF interface is excellent and very user friendly - but I prefer to use native WordPress functionality to retrieve and display data that has been stored by ACF. This ensures that the data will still be displayed even if the client disables or removes the ACF plugin.
This tutorial documents how to retrieve and display a repeater field called downloads
with a single subfield called file
.
The file attachment IDs are retrieved, and these are used to build an array that contains the title, permalink, file type and file size.
This array is used to construct a list of “click to download” links.
Class Methods
This example shows the data-retrieval function and a function that builds a ul
element to display files for download.
I have shown them as static methods in a class, but if you are using procedural programming rather than an OOP approach you could drop the functions in your theme’s
functions.php
file.
If you do this, you should prefix your functions with a pseudo namespace, and change the reference to self::get_repeater_file_download()
in the_attached_downloads()
.
Retrieve the Data
This method fetches data from the postmeta
table and returns a multi-dimensional array.
Display Data
This builds the HTML for a list of downloadable links using data from the method above.
The download attached files <ul>
can be displayed within template files by calling <?php ClassName::the_attached_downloads(); ?>
.
comments powered by Disqus