In Part 1 of this Advanced Custom Fields (ACF) WordPress plugin tutorial series we taught you how to add custom fields to a custom post type using the Advanced Custom Fields (ACF) WordPress plugin and a PHP function. In part 2 of this two-part tutorial we will teach you how to display the custom fields you created for your Custom Post Type in your WordPress theme template file using PHP.

Use PHP to display the custom fields for your custom post type in the WordPress theme template file

In order to display your custom fields on your actual WordPress site you will need to use PHP to access the global variable and retrieve them from the database. Open the archive theme template for your custom post type (ex. archive-YOUR-CUSTOM-POST-TYPE.php). Use the PHP code below as a base example for how to display your custom field, please refer to the ‘Items to Note’ below for detailed setup instructions.

PHP

<?php $YOUR-CUSTOM-FIELD = get_field('YOUR-CUSTOM-FIELD', 'option'); if ($YOUR-CUSTOM-FIELD) { echo $ YOUR-CUSTOM-FIELD; } ?>

Items to Note:

  • Make sure that you change where it says ‘YOUR-CUSTOM-FIELD’ to match the custom field name you used in the backend.
  • Please keep in mind that this is a basic example of how to output the custom field variable and you may need to modify it to suit your needs.

This is PART 2 of a two-part WordPress tutorial, if you missed PART 1 please click on the button below to get started:

In part 1 of this two-part tutorial we are going to teach you how to add custom fields to a custom post type using the Advanced Custom Fields (ACF) WordPress plugin and a PHP function.

View Part 1

Troubleshooting

If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. Please do not email us with problems regarding this tutorial, only comments will be responded to.

Share This