Displaying the Fields of Advanced Custom Fields Plugin
Advanced Custom Fields (ACF) lets you add extra information to your posts, such as power, voltage, material, or warranty. This recipe displays selected fields below the main content on a single post, as either a table or a description list.
The example targets RT Product Showcase posts. You can adapt it for blog posts, portfolio items, or WooCommerce products by changing the post type.
Before you start
Section titled “Before you start”- Install and activate Advanced Custom Fields.
- Use an active child theme and back up its
functions.phpfile before editing. A child theme keeps this customization separate from parent-theme updates. - Try the change on a staging site first. You will be adding PHP code to your child theme.
This example is for Text and Number fields. It outputs plain text; images, links, repeaters, galleries, and other structured fields need their own display code. ACF values have different return formats depending on the field type.
1. Create your fields
Section titled “1. Create your fields”-
In WordPress, open ACF → Field Groups and choose Add New or Add Field Group.
-
Name the group Product Specifications.
-
Add these example fields:
Field label Field name Field type Example value Power powerText 12 W Input Voltage input_voltageText 220–240 V Material materialText Aluminum Warranty warrantyText 2 years -
Under Location Rules, set Post Type → is equal to → your Product Showcase post type. If you use another post type, select it here instead.
-
Save the field group.
-
Edit a post of that type, fill in its new fields, and save or update the post.
The field name, such as input_voltage, must match the name in the code below. The field label is the friendly name shown in the editor. See ACF’s field-group guide for details.
2. Add the display code
Section titled “2. Add the display code”Open the active child theme’s functions.php file, usually at wp-content/themes/your-child-theme/functions.php. Add the following code inside the existing PHP section. Do not add another <?php opening tag. If the file has a closing ?> tag, put the code before it.
Use this snippet once. If you previously added the older rt_display_acf example, remove that snippet first to avoid showing the fields twice.
add_filter( 'the_content', 'rtthemes_recipe_display_acf_fields', 20 );
function rtthemes_recipe_display_acf_fields( $content ) { $target_post_type = 'products'; $layout = 'table'; // Use 'list' for a description list.
if ( is_admin() || is_feed() || ! is_singular( $target_post_type ) || get_post_type() !== $target_post_type || ! in_the_loop() || ! is_main_query() || post_password_required() || ! function_exists( 'get_field' ) ) { return $content; }
// Display label => ACF field name. Add your own pairs here. $my_fields = array( 'Power' => 'power', 'Input Voltage' => 'input_voltage', 'Material' => 'material', 'Warranty' => 'warranty', );
$items = ''; $post_id = get_the_ID();
foreach ( $my_fields as $label => $field_name ) { $value = get_field( $field_name, $post_id );
// Skip missing values and types this plain-text example cannot display. if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) { continue; }
if ( trim( (string) $value ) === '' ) { continue; }
if ( 'list' === $layout ) { $items .= sprintf( '<dt>%s</dt><dd>%s</dd>', esc_html( $label ), esc_html( (string) $value ) ); } else { $items .= sprintf( '<tr><th scope="row">%s</th><td>%s</td></tr>', esc_html( $label ), esc_html( (string) $value ) ); } }
if ( '' === $items ) { return $content; }
if ( 'list' === $layout ) { return $content . '<dl class="rt-acf-specifications">' . $items . '</dl>'; }
return $content . '<table class="rt-acf-specifications"><tbody>' . $items . '</tbody></table>';}The snippet keeps the existing content and adds only the fields you list. Blank fields are hidden, while numeric values such as 0 remain visible. If ACF is inactive, the original content is returned.
It uses WordPress’s content filter with checks for the main loop. Labels and values are escaped with esc_html() so they display safely as text, following WordPress’s escaping guidance.
3. Choose your post type and layout
Section titled “3. Choose your post type and layout”Choose where the fields appear
Section titled “Choose where the fields appear”Change $target_post_type = 'products'; to the post type you want:
| Content | Post type in the snippet |
|---|---|
| RT Product Showcase, as used in the original example | products |
| WooCommerce products | product |
| RT Portfolio, as used in the original example | portfolio |
| Regular blog posts | post |
| WordPress pages | page |
Custom post type names can vary between themes and plugins. Confirm the registered name on your site, and use the same post type in your ACF location rule. Note the difference between products and WooCommerce’s product.
This method appends fields wherever the template renders the main post content through the_content. On a standard WooCommerce product template, that is usually the Description tab. It does not add fields beside the price or to the short description. A template or page builder that bypasses the main content filter needs a different integration.
Display as a table
Section titled “Display as a table”Keep $layout = 'table';. Each specification appears in a row with its label on the left and value on the right:
| Specification | Value |
|---|---|
| Power | 12 W |
| Input Voltage | 220–240 V |
| Material | Aluminum |
| Warranty | 2 years |
Display as a description list
Section titled “Display as a description list”Change the same line to $layout = 'list';. The snippet outputs a <dl> with each label in a <dt> and each value in a <dd>. The theme determines its spacing and appearance. Both layouts use the rt-acf-specifications class if you want to style them later.
Add more specifications
Section titled “Add more specifications”Add another label and field-name pair inside $my_fields, for example:
'Lumens' => 'lumens','Rated Life (Hours)' => 'rated_life_hrs','Certificate' => 'certificate',Create the matching fields in ACF and enter values for each post. The order of the pairs determines the display order.
For a field such as Dimmable, use a Text field with Yes or No for this recipe. An ACF True / False field returns a boolean and is deliberately skipped by this snippet; it requires an explicit conversion to a readable label.
4. Check the result
Section titled “4. Check the result”Open the single post on your website. Your specifications should appear after its main content. Then check a different post type to confirm that its content still displays normally.
- Leave one specification empty: its row or list entry should disappear.
- If you use a Number field, enter
0: it should still appear. - Switch between
tableandlistto confirm your preferred layout.
Troubleshooting
Section titled “Troubleshooting”The fields do not appear. Confirm ACF is active, the field group is active, the field names match $my_fields, and you saved values on the post you are viewing. Check the post type and location rule. Archives and previews rendered outside the main loop are excluded. If your template or page builder does not use the main content filter, use its supported dynamic-field feature or a custom template integration.
A field displays an ID or is missing. This example handles plain text and numbers only. Images and other structured fields can return IDs, arrays, or objects; do not add them to this snippet without adapting the output for that field type. HTML entered into a text field is displayed as text.
The fields appear twice. Check for another copy of this filter, the older rt_display_acf snippet, or a builder widget already displaying the same fields. A template that renders the main content twice can also run the filter twice.
The website reports a PHP error after saving. Restore your backup using your hosting file manager or SFTP, then check the placement of the code and any edits to quotation marks, commas, or braces.
For other field types, start with ACF’s displaying values guide.