0
0
Wordpressframework~5 mins

Advanced Custom Fields plugin in Wordpress

Choose your learning style9 modes available
Introduction

The Advanced Custom Fields (ACF) plugin helps you add extra information fields to your WordPress posts, pages, or custom content easily without coding complex database changes.

You want to add custom input fields like text, images, or dates to posts or pages.
You need to collect extra details from users when they create content.
You want to display special information on your website that is not part of the default WordPress fields.
You want an easy way to manage custom data without writing PHP code.
You want to create flexible content layouts with repeatable or conditional fields.
Syntax
Wordpress
<?php
// To get and display a custom field value in a template
$value = get_field('field_name');
echo $value;
?>
Use get_field('field_name') to retrieve the value of a custom field created with ACF.
Field names are the unique identifiers you set when creating fields in the WordPress admin.
Examples
This example shows how to get and print a simple text custom field called 'subtitle'.
Wordpress
<?php
// Display a text field value
$text = get_field('subtitle');
echo $text;
?>
This example retrieves an image field and outputs an HTML image tag with the image URL and alt text.
Wordpress
<?php
// Display an image field
$image = get_field('header_image');
if ($image) {
  echo '<img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '">';
}
?>
This example shows how to safely check if a custom field has a value before printing it.
Wordpress
<?php
// Check if a field has a value before displaying
if (get_field('event_date')) {
  echo 'Event Date: ' . get_field('event_date');
}
?>
Sample Program

This code snippet fetches two custom fields: a testimonial text and its author. If both exist, it displays them nicely formatted. Otherwise, it shows a fallback message.

Wordpress
<?php
/* Template part to show a custom testimonial with ACF fields */
$testimonial = get_field('testimonial_text');
$author = get_field('testimonial_author');
if ($testimonial && $author) {
  echo '<blockquote>' . esc_html($testimonial) . '</blockquote>';
  echo '<p><strong>-- ' . esc_html($author) . '</strong></p>';
} else {
  echo '<p>No testimonial available.</p>';
}
?>
OutputSuccess
Important Notes

Always sanitize output with functions like esc_html() or esc_url() to keep your site safe.

ACF fields are created in the WordPress admin under Custom Fields, no coding needed to add fields.

Use the ACF documentation and field groups to organize your custom fields logically.

Summary

ACF lets you add and manage extra fields easily in WordPress without coding database changes.

Use get_field() in your templates to get and show custom field values.

Always check if fields have values before displaying to avoid errors.