Custom fields in WordPress: Custom Post Type and Advanced Custom Fields
How to add custom field types to a custom post type in WordPress with Advanced Custom Fields and display them in a single-post template.
WordPress
We show how to add custom fields (text, images and more) to a custom post type in WordPress with the Advanced Custom Fields (ACF) plugin, and how to display them in a single-post template. At the end we have a finished “Projekt” post type with custom fields and a single-projekt.php template.
The screenshots show the Polish WordPress admin interface. Field names in the code stay as shown.
Requirements
-
The Advanced Custom Fields plugin, which lets you add custom field types.

-
Knowledge from the previous tutorial, creating a template for a custom post. It explains how to create a custom post type with the CPT UI plugin.
-
Basic knowledge of PHP. We do not use complicated functions, but you need the basic syntax of the language to follow the code.
Creating the “Projekt” custom post type
After installing ACF, we create a new post type: Projekt. We want to build a list of projects as a portfolio. Each project should contain the following elements:
- title,
- short project description,
- project description,
- year the project was created,
- scope of the work we did,
- featured image,
- project images.
In the CPT UI plugin, we add a new post type with these settings (if you do not know how, see the previous tutorial):
- Slug:
projekt, - Plural: Projekty,
- Singular: Projekt.
With the built-in CPT UI options, we enable the basic post elements: title, content editor, excerpt and featured image. They cover the title, description, short description and featured image from the list above.

Adding custom fields in ACF
We add the remaining fields in ACF. In the WordPress admin, we open Custom Fields → Add New (Polish UI: Własne pola → Dodaj nową). In newer plugin versions, this menu item may be called “ACF”.
First, we give the field group a title. We suggest a name made of the singular form of our custom post type and the word Inputs, so that it is clear at a glance that these are custom fields. Here it is Projekt Inputs.

We click the button that adds a field, choose the field type and fill in its settings:
- Field label is the name visible only in the WordPress admin. It may contain spaces and any characters. We choose a label that says what the field is for, for example “Co wykonaliśmy w projekcie” (“What we did in the project”).
- Field name works like the slug of a post type. It must not contain spaces or non-ASCII characters, and we use it in PHP code. We choose a short name, for example
wykonanie. - Field type: the available types are described in the plugin documentation. For the
wykonanieandrokfields we use the Text type. - The remaining settings are optional and let us adapt data entry to the project. We do not need them in this example. The documentation describes them in detail.
In this example we create four fields: wykonanie and rok (type Text), and obraz_1 and obraz_2 (type Image). For the image fields we keep the default return format, which is the image array (Image Array). We use this format in the template.
After creating all fields, we add a location rule for the field group:
Post Type → is equal to → Projekt (our custom post type).

Adding sample projects
The post type and custom fields are ready. Now we add a few sample projects.


Template for a single project
We have a few preview projects. Now we create a template for a single project that displays all fields.
We work on an empty wp_tutorial theme created for this tutorial. If you do not know how to build your own theme, see the tutorial on a theme based on Underscores. We copy the single.php file and add the slug of our custom post type to its name. This gives us the single-projekt.php file. WordPress uses it automatically for posts of the projekt type (according to the template hierarchy).
We edit the file:
<?php
/**
* The template for displaying a single "projekt" post.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package wp_tutorial
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
while ( have_posts() ) : the_post();
?>
<h2 class="tytul-projektu"><?php the_title(); ?></h2>
<div class="krotki-opis-projektu"><?php the_excerpt(); ?></div>
<span class="wykonanie"><?php echo esc_html( get_field( 'wykonanie' ) ); ?></span>
<span class="rok"><?php echo esc_html( get_field( 'rok' ) ); ?></span>
<div class="obraz-glowny"><?php the_post_thumbnail( 'full' ); ?></div>
<div class="opis-projektu"><?php the_content(); ?></div>
<div class="obrazy">
<?php
// Both image fields are rendered the same way, so we loop over their names.
foreach ( array( 'obraz_1', 'obraz_2' ) as $field_name ) :
$image = get_field( $field_name );
if ( $image ) :
$alt = $image['alt'];
$size = 'large';
// Fall back to the full-size URL if the "large" size was not generated.
$thumb = isset( $image['sizes'][ $size ] ) ? $image['sizes'][ $size ] : $image['url'];
?>
<img src="<?php echo esc_url( $thumb ); ?>" alt="<?php echo esc_attr( $alt ); ?>" />
<?php
endif;
endforeach;
?>
</div>
<?php
// End of the loop.
endwhile;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
This displays all fields of the “Projekt” custom post type.
Text fields
We read a field value with get_field( 'field_name' ). The the_field( 'field_name' ) function does the same but prints the value right away without escaping it. That is why we use get_field() together with esc_html(). This is how we display the wykonanie and rok fields:
<span class="wykonanie"><?php echo esc_html( get_field( 'wykonanie' ) ); ?></span>
<span class="rok"><?php echo esc_html( get_field( 'rok' ) ); ?></span>
Image fields
We also use get_field() to read the images. An image field can return an array, a URL or an attachment ID. ACF returns an array by default. From this array we take the alternative text and the image URL in the chosen size.
$image = get_field( $field_name );
$altholds the alternative text of the image,$sizesets the image size,$thumbholds the image URL in that size (or the original URL if the size is missing).
$alt = $image['alt'];
$size = 'large';
$thumb = isset( $image['sizes'][ $size ] ) ? $image['sizes'][ $size ] : $image['url'];
The esc_attr() and esc_url() functions escape the printed attribute and URL:
<img src="<?php echo esc_url( $thumb ); ?>" alt="<?php echo esc_attr( $alt ); ?>" />
Note: the get_field() and the_field() functions come from the ACF plugin. If the plugin is deactivated, the template raises a fatal error. In templates that must work without ACF, it is worth guarding these calls with function_exists( 'get_field' ).
The result is a view with all the added fields.

Summary
Combining CPT UI and ACF lets us add custom content types with any fields to WordPress without building an admin interface from scratch. Displaying the fields takes only basic PHP and HTML and a few WordPress and ACF functions. Images need the most attention, because the field returns an array with several values. Once we know get_field() and the escaping functions esc_html(), esc_attr() and esc_url(), we can display any other field type the same way.