Our clients often need a products page or a team page that displays collections of dynamic content that include various types of data (text, images, charts, pdf downloads, etc.) per item. Sometimes we’ll even be asked to show various pieces of those items in multiple locations on the site – a menu of the collection, for example.
There’s often no one right way to do things. Though, when using WordPress, we’ve found a method of achieving this while getting ultimate customization and reusability, as well as an intuitive, fool-proof admin experience.
Let’s take the products page as an example. We recently had a client with a particular product suite in which a customer would choose a package that would always include a main component. The customer could then review, compare, and select a combination of multiple sub-components. Each sub component needed a description, image, and spec chart that would display on the page, ideally dynamically, in a responsive layout with an intuitive user experience that could also be easily managed in the back end.
Our method utilizes the indispensable plugin, Advanced Custom Fields, in combination with custom post types to create an easily managed, easily queried set of posts that can contain any type of data needed. Let’s look at how to quickly set up something similar.
Install the Plugin
First, and perhaps it goes without saying, we’ll need a WordPress install. Second, if you install no other plugin, install Advanced Custom Fields https://www.advancedcustomfields.com/. Version 4.4 is what is available on the WordPress Plugin Directory at the time of this writing, but we happen to know that if you want to go for a paid pro account, Version 5 has some pretty killer features. For our purposes here, the free version is all we’ll need.
Register Your Custom Post Type
The next step deals with WordPress’ standard custom post type registration. Here, we’ll register the Products post type. For more information on registering custom post types, parameters, and custom taxonomies, see https://codex.wordpress.org/Function_Reference/register_post_type. Add this to your functions.php file.
Set Up Advanced Custom Fields
Now that we have our newly created custom post type, we can start to create all of the products we want as individual posts. But what if we want each product to include a title, main description, a downloadable pdf of specifications, and two images? We could cram all of that in the one wysiwyg WordPress gives us and hope for the best, but that’s not going to be the most reliable and easily managed method of building our back end. This project sounds like we need some custom fields. First, head to the Custom Fields admin page and add a new field group. Call this group ‘Product Info’ and add a new rule in the Location box that reads ‘Show this field group if Post Type is equal to Product’. If you do not see Product as an option to select, go back and make sure you registered your custom post type correctly.
Next, start adding fields to the group. This tutorial assumes some level of familiarity with the plugin, but if you aren’t familiar with Advanced Custom Fields, have a look at their documentation: https://www.advancedcustomfields.com/resources. Because each product post will already have a title, a content wysiwyg, and a featured image, we can use those as product title, product description, and one of the images. For the other product features, add a File field called ‘Download’ with a return value of File Array, and an Image field called ‘Product Image’ with a return value of Image URL.
Querying the Products
After adding a few products, we need to output them somewhere. This can be done in whichever way works best for your site. You can make a template php page that queries the posts, or use a custom shortcode; regardless, you are now in a position use any part of the products data in any way you wish. Here is an example of a basic query:
This is a very simple query that will output all of the products entered, but you can see that any of these elements can be queried on any page. Everything is managed on the backend in one place and the opportunities for reusing the data are endless. One query can be used to create a menu of products as well as each product output. A product that relates to a particular blog article can be queried on that article’s single view page. If you are into the new WordPress api, this will be exposed for use there as well.
If your WordPress site needs more flexible data with an easy-to-manage back end, Advanced Custom Fields is the perfect solution for visually creating fields, selecting multiple input types, assigning fields to multiple edit pages, easily loading data through a clear and familiar interface, and improving upon native WordPress custom post type and metadata features for ease and fast processing. If you need help building, customizing, or refreshing your web presence, Fresh can help.