Overview
The Advanced Custom Field’s API makes it very easy to display field data in your theme. There are many functions available and all are well documented on the resources page.
The Basics
Once you have created a field group and input some data, you can now load and display the data in your theme.
All values are saved as native post_meta (when saved to a post) and although you can use the native WP function get_post_meta()
, it is better practice to use the relevant ACF function such as get_field()
. Why? Because ACF will format the value depending on the field type and make development quicker and easier!
To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field.
To display a field, use the the_field() in a similar fashion.
Here’s a basic usage example, and please be sure to view the code example page for more.
<?php
/**
* Template Name: Home Page
*/
get_header();
?>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_field('custom_title'); ?></h1>
<img src="<?php the_field('hero_image'); ?>" />
<p><?php the_content(); ?></p>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Does ACF have a Shortcode?
Yes, you can use it in the same way as the the_field() function. It looks something like this:
[acf field="{$field_name}"]
You can also specify the $post_id to fetch the value from.
[acf field="{$field_name}" post_id="{$post_id}"]