0
0
WordpressHow-ToBeginner · 4 min read

How to Use ACF Plugin in WordPress: Simple Guide

To use the ACF (Advanced Custom Fields) plugin in WordPress, first install and activate it. Then create custom fields in the WordPress admin, assign them to posts or pages, and display their values in your theme using get_field() or the_field() functions.
📐

Syntax

The main functions to get and display ACF custom field values in your theme are:

  • get_field('field_name'): Returns the value of the custom field with the given name.
  • the_field('field_name'): Echoes (prints) the value of the custom field directly.

You use these inside your theme PHP files, like single.php or page.php, to show custom data.

php
<?php
// Get the value of a custom field named 'subtitle'
$subtitle = get_field('subtitle');

// Display the subtitle if it exists
if ($subtitle) {
    echo '<h2>' . esc_html($subtitle) . '</h2>';
}

// Or simply print the field directly
the_field('subtitle');
?>
💻

Example

This example shows how to create a custom field called 'subtitle' in ACF and display it below the post title in a single post template.

php
<?php
// Inside single.php or a template part
get_header();

if (have_posts()) :
    while (have_posts()) : the_post();
        the_title('<h1>', '</h1>');
        // Display the custom field 'subtitle'
        $subtitle = get_field('subtitle');
        if ($subtitle) {
            echo '<h2>' . esc_html($subtitle) . '</h2>';
        }
        the_content();
    endwhile;
endif;

get_footer();
?>
Output
<h1>Post Title</h1> <h2>Custom Subtitle Text</h2> <p>Post content here...</p>
⚠️

Common Pitfalls

  • Not activating the ACF plugin after installation will cause get_field() to return nothing.
  • Using the wrong field name in get_field('wrong_name') returns null.
  • Trying to use ACF functions outside the WordPress loop or before the post is set can cause empty results.
  • Not escaping output can lead to security issues; always use esc_html() or similar.
php
<?php
// Wrong way: Using wrong field name
$wrong = get_field('wrong_name'); // returns null

// Right way: Use correct field name and check
$correct = get_field('subtitle');
if ($correct) {
    echo esc_html($correct);
}
?>
📊

Quick Reference

FunctionDescriptionUsage Example
get_field('field_name')Returns the value of a custom field$value = get_field('subtitle');
the_field('field_name')Prints the value of a custom fieldthe_field('subtitle');
have_rows('field_name')Checks if a repeater field has rowsif (have_rows('gallery')) { ... }
the_row()Moves to the next row in repeater fieldswhile (have_rows('gallery')) { the_row(); }

Key Takeaways

Install and activate the ACF plugin before using its functions.
Create custom fields in the WordPress admin and assign them to posts or pages.
Use get_field() to get values and the_field() to print them in your theme templates.
Always check if a field has a value before displaying it to avoid errors.
Escape output with esc_html() to keep your site secure.