Through the Custom Fields, Iterating AlongDecember 17, 2014
I discovered the plugin Advanced Custom Fields last summer. This is such a powerful plugin. I immediately bought the Repeater Field addon. With repeater fields you can make rows of the same type of field. Loops of fields.
An example of a nice way to use this plugin combo would be a page of customer testimonials on a website. In the past I would have made a Custom Post Type called Testimonial, and made a page to list all the testimonials, with an intro and a
WP_query loop. But using Repeater Fields is much nicer; its simpler to code and has easier content management.
To start, make a page in your WordPress site called Testimonials, and write an intro.
Now you need to have the two plugins mentioned above installed. Go to Custom Fields in your Admin area, and make a new Field Group. Call it Testimonial Fields, and under field type choose repeater.
You can add any number of sub-fields to a repeater field. It depends on your design. I used 4 sub-fields in a recent site: pullquote, testimonial text, attribution, and video link. Note that you can label the fields helpfully, and drag them to re-sequence them! These are the touches that make this plugin so awesome.
There is metabox called Location where you can specify exactly where you want these fields to show up. So set your Testimonial Fields to display on the Testimonials page.
Then you need to make a testimonials template in your theme. If you duplicate your page.php file and rename it page-testimonials.php that will work.
The code that loops through the Testimonials is very simple. Paste this into your new template file, somewhere below
This setup is so much better than using a custom post type! Unless every testimonial needs its own permalink that is. Testimonials can all be edited on one page, and re-sequenced by drag and drop.
##Now add a little jQuery
Another lovely use for Repeater Fields is making client-editable accordions. I generally make custom post types for staff pages on business sites—because each staff member need their own URL. And often these staff members have 2 thousand word resumes. When posting very long content online, an accordion is helpful to show and hide sections one at a time. See this demo at a law firm.
This is how I do it: make a Repeater Field called Resume. It has two sub-fields: Resume Heading (
resume_heading) and Resume Content (
Use this code to display the list of headings. The first list item is a link to the Bio, which is not in a custom field; its simply the regular content section of the post. The resume section will hide it when they display.
We can make the anchor links from the
resume_heading field by passing the string through the WordPress sanitizing function
This code displays the content.
It needs CSS to initially hide the resume:
And some sweet jQuery to make it work: