How-To Create WordPress Custom Post Type Archive Content With CMB2

Published: May 18, 2016  by 

how to create custom post t ype archive content with cmb2

A common issue web developers often run into is how to get content into archives for custom post types. This often happens when either the SEO team or Content Marketing team wants to add content to an archive page. Archives, by their nature, only display archive content. There is no custom title or custom content.

One solution is to create a page with the custom post type name, query for that page title and display the content. As a developer, I cringe at that hacky solution. No bueno!

Let’s use one of my favorite WordPress plugins, CMB2, to accomplish this task in a more native, less hacky way.

CMB2 Introduction

For those not familiar with CMB2, it is a field library. CMB2 allows you to create custom fields programmatically, and its flexibility and data storage is far more superior than that of Advanced Custom Fields. For this example, we’re going to assume you have CMB2 installed as a plugin for WordPress.

First, let’s get to the plugin you’ll need to add options to your custom post type archives.

Plugin Structure

Plugin Directory Structure
Plugin Directory Structure

In this example, we’re going to name our plugin folder custom-theme-types.

Inside the custom-theme-types folder, we use the structure:

    • custom-theme-types.php (our main plugin file)
    • class-cpt-sub-menus (class to create the sub-menus)
Plugin Internal Structure
Plugin Internal Structure

CMB2 Plugin Code

Let’s dive right into the plugin code, which will be found in custom-theme-types.php.

In the above code, we’ll dissect a few areas.

On line 28, we create an array of post types that need a sub-menu. We include class-cpt-sub-menus.php on line 32.

Finally, on line 33, we do a for each and instantiate a new sub-menu for each post type we want support for.

The result, with the plugin activated, will look like this:

Gallery Sub-Menu
Gallery Sub-Menu

So you’re probably curious by now what’s in class-cpt-sub-menus.php. Well, I don’t disappoint.

The above code is modified from the original version, which can be found in the CMB2-Snippet-Library.

I encourage you to look over and understand the code, but the simplest way to describe the code is that it adds a sub-menu to the custom post type menu item, and starts adding fields on line 100.

The above code will create a sub-menu item that looks like this:

Gallery CPT Options
Gallery CPT Options
Gallery Options Filled Out
Gallery Options Filled Out and Saved

Theme Files

Now that the plugin works and data is being saved, how do we find the archive options? Well, since our post type name is gallery, we create a file in our theme called archive-gallery.php.

From this archive-gallery.php file, we call in our options and display them if they are available:

Custom Title and Content Displayed
Custom Title and Content Displayed

Time to Test Drive

You can download the plugin file here: Custom Theme Types

Also combine with Custom Post Type Templates to pack more of punch.

Whether you’re an old pro and know this process like the back of your hand, or if you have any further questions, we’d love to hear from you. Feel free to reach out to us here or via social media and let’s get a conversation rolling!