Advanced Custom Fields – custom field types WordPress
Thanks to custom field types, we can add properties, images and any other content to our own post type that would determine additional functionality of our entry. In this tutorial you will learn how to add such fields to a given type of posts.
How do I add custom field types for Custom Post Type using Advanced Custom Fields in WordPress?
What do we need?
- Advanced Custom Fields Plugin – a plugin that allows you to add your own field types,
- Knowledge from previous tutorial – Creating a template for custom post – if you don’t know how to create a Custom Post Type using CPT UI I’m sending you back to this tutorial,
- Basic language knowledge PHP – no complicated functions will be used here, but you will need to familiarize yourself with the basic language syntax to be able to understand the code that will be in this guide.
Let’s do it! Creating custom fields with advanced custom fields in WordPress
After installing ACF (Advanced Custom Fields) plugin we move on to create a new post type – Projekt. We will want to create a list of projects in the form of a portfolio, which will contain several basic fields:
- Brief project description,
- Description of the project,
- The year of the project,
- What we did in the project,
- The distinguishing image,
- The images of the project
Go to CPT UI and add a new type of post (if you don’t know how to do it I refer to the previous tutorials):
- Slug -> projekt,
- Plural -> Projekty,
- Singular -> Projekt
Let’s use the built-in CPT UI option to create some basic fields for our custom post.
After creating Custom Post Type -> Projekt and adding the basic fields we proceed to add the remaining fields using Custom Fields. Go to the tab in the WordPress panel: Friendly fields -> Add new. Please give a title to a group of fields. Here I suggest that the naming should not get mixed up in the further part of creating the project to name from the single number of our Custom Post to which we want to add the field data and add a name Inputs so that you know that it is about your own field types. In our case it will be: Projekt Inputs.
Click the add field button and select the desired field type and name it.
Field text -> the name displaying the field, there may be Polish characters and spaces – will be visible only in the WordPress panel. Give this label so you know what the field is responsible for, e.g. ”What we did in the project”.
Field name -> Like the service for posting, it cannot contain Polish characters and will be used in PHP code. Give an easy field name, e.g. “wykonanie”..
Field type -> field types are different, they are described in plugin documentation. In our case it will be for example Text.
The rest of the -> fields; they are optional and we can use them to customize the field input for our project, but in our case this will not be necessary. For more information, see documentation.
Remember to add Condition for a given group of fields after creating all field types:
The type of entry -> it is equal to -> Projekt (Custom Post Type, with us type: Projekt).