We love Advanced Custom Fields. We use them on pretty much every Word Press project. I’d like to start sharing some tips that I find useful that have to do with the greatness that is ACF.
One thing I like to do when creating a new template file is to organize most of my PHP data towards the top and use it as needed further below.
A standard Word Press template starts life out in the simplest of forms:
<?php /* Template Name: Test Template */ get_header(); the_post(); ?> <?php get_footer(); ?>
If I am creating a template that uses quite a few acf fields, my next step is to call all fields associated with the page:
<?php /* Template Name: Test Template */ get_header(); the_post(); $fields = get_fields(); ?> <?php get_footer(); ?>
A lot of people call each field individually via $test = get_field(‘my_field’). While that will work, I prefer to obtain all fields with just 1 variable call. Now any fields for that post ID should be available in array form – aka:
<?php /* Template Name: Test Template */ get_header(); the_post(); $fields = get_fields(); $title = $fields['page_title']; $color = $fields['page_color']; $best_web_dev = $fields['it_is_nick']; ?>
To me, this allows you to quickly scan the top of the template file to see what data is being manipulated instead of having to hunt through a bunch of HTML. It pretty much comes down to user preference on how you want to set up your templates, but I have found this way to be the most efficient.