WordPress has certainly come a long way dating back from its humble beginnings in the blogging arena. With Custom Posts Types (CPTs) becoming popular, WordPress has risen to become a highly functional CMS platform is suitable for just about type of content.
However, when you create a new CPT, WordPress just provides you with default standard fields like editor box, title and so on, which are inadequate if you require much more than you were to get had you used a Page or Post type.
The “Custom Fields” meta box on WordPress can be used, but this requires naming each filed for every new post. You will of course want to keep things very simple for yourself and your followers, and if the CPT needs to keep the same fields for each post, you’ll need a more robust and simple approach for adding the fields.
Begin By Creating A Custom Post Type
WordPress has many amazing plugins to help you automate this task, like Scott Clark’s Pods and Brad William’s Custom Post Type . But these however add some level of complexity to something relatively simple. So, when you create custom post types, the best approach may be to just go with the code.
You can create a dedicated plugin, something in the line of Project CPT, and place all the custom taxonomy, custom post type as well as other linked functionality there. You can also put this in your theme’s function.php but this would blur the link between data and presentation. You may decide to redesign your site, but then would also prefer to keep the custom post types that are you are creating now
Listen to the Latest Episodes Right Now.
Downloading, Installing and Activating Advanced Custom Fields
Advanced Custom Fields are great plugins to use as they give you a graphical interface when creating custom fields. You build sets of fields then assign the sets to your custom post types. You can get them for free from the WordPress Plugin Directory, but can also purchase premium features like Flexible Content Field, Repeater Fields and more.
Creating Your Field Group
ACF comes with a variety of field types which you can utilize in your CPT, and these include simple text fields, file upload, a WYSIWYG editor, image and more.
For example, a “book” custom post type will utilize the post title for the book title as well as the editor box for the book synopsis. This leaves the need to build the following fields:
- Author: Text, without any HTML formatting
- Publishing: Text, without any HTML formatting
- Cover: Image
- Copyright date: Numeric (no need for a date since you’ll be listing for a year)
- Link to Amazon: Text, without HTML formatting
On the dashboard, click “Add New” on the Custom Fields tabs to create a new field called “Books.” Click on “Add Field” in order to create the above fields needed.
Assigning Your Field Group to A Custom Post Type
On the “Location” area, you can setup a criteria to which the field group will appear. For our purpose, we’ll choose “Post Type” which is the same as “book” but you also have the option to apply field groups that are based on specific pages and posts with certain tags or categories, which page theme is being used and so on. If necessary, the field group will appear dynamically when the correct criteria is met, like assigning posts to particular categories.
Choosing Your Display Options
You can specifically configure the way you want field groups displayed in the last panel, like in a metabox or not. And whether you want it displayed in the right sidebar of the dashboard or in the main column. You also have the option to have default WordPress inputs invisible when a field group is displayed.
Once you are satisfied with creating the field group, click on “Publish” to save and activate. Your field group will then be able to appear on the edit or add post screen as long as it meets the specified criteria.
Using the Custom Fields
You can easily use the custom fields that you create in your template. WordPress provides native template tags that are within the Loop, like..the_title () for post titles, the_content() for all content put in the editor box and so on. Advanced Custom Fields give you the_field( ‘field_name’), of which the ‘field_name is a machine readable “Field Name” for a custom field to display whatever field you want. If you however want to return that field for more processing instead of displaying it directly, then use the get_field( ‘field_name’) function. To add on, custom fields are also accessible as short codes.
To complete some documentation when you use custom fields in your WordPress themes, you can use ACF’s thorough method of documentation.
Custom Post Types come as an excellent attribute of WordPress that gives it the ability to perform several extraordinary things. However, this power would not mean anything without the robust yet intuitive process of adding custom fields. The good thing is that, Advanced Custom Fields give users the functionality that WordPress in its core stake lacks, and this is one crucial element that can make an indispensable part of any workflow.
There are many benefits that come with using custom fields on WordPress, and the process of adding them to your WordPress site can be made easy by using the above mentioned strategies.