Adding A Title To A Custom Post Type Archive Page

Generally, custom post type archive pages like portfolio archives, don’t include a title. The reason for this is there’s no page template created when the custom post type is generated from the functions.php file. So you can’t simply add a title like you can with a page, post or standard archive page like which categories, tags and author archives use.

Rather than bore you with the reasons, i’ll simply give you the code which works.

Update: You can now add a archive settings page to any CPT using Genesis 2.0. Simply add these

'has_archive' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'genesis-seo', 'genesis-cpt-archives-settings' ),

Here’s an example of what the full code snippet should look like:

/** Create portfolio custom post type */
add_action( 'init', 'portfolio_post_type' );
function portfolio_post_type() {
	register_post_type( 'portfolio',
		array(
			'labels' => array(
				'name' => __( 'Portfolio', 'child theme text' ),
				'singular_name' => __( 'Portfolio', 'child theme text' ),
			),
			'exclude_from_search' => true,
			'has_archive' => true,
			'hierarchical' => true,
			'menu_icon' => get_stylesheet_directory_uri() . '/images/icons/portfolio.png',
			'public' => true,
			'show_ui' => true,
			'show_in_menu' => true,
			'rewrite' => array( 'slug' => 'portfolio' ),
			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'genesis-seo', 'genesis-cpt-archives-settings' ),
		)
	);
}
Note: This relies on custom permalinks set up in WordPress under Settings > Permalinks. You may have to resave them after setup of CPTs or changing the code.

This code will come in handy when using Genesis child themes like the Executive theme by StudioPress which uses a custom post type for the portfolio.

This code will display your custom post type archive title for your custom post type in the same way your category archive titles are displayed in the genesis_before_content hook position using the h1 heading size.

This code will also display your custom post type archive title but in the genesis_after_header hook position and aligns the title to the centre with a h2 heading size.

This code does the same as the code above however can be used for multiple custom post type archive titles. Not sure why you’d want to use the same titles on different custom post type archive pages but you may do for some reason.

The conditional tag used is:

is_post_type_archive

You can use different hooks with this code however you’ll find the genesis_post_title hook will also include the archive page title on posts attached to that custom post type archive which won’t be ideal.

This is the reason i have included the genesis_after_header and the genesis_before_content hooks as they only display the custom post type archive page title on the archive page and not on all the posts titles included in that archive.

Related Tutorials


Comments

3 responses to “Adding A Title To A Custom Post Type Archive Page”

  1. Ryan Dolan Avatar
    Ryan Dolan

    Hey Brad adding a portfolio archive page name is exactly what I am looking for. One thing i am struggling with though is that i am using the minimum pro theme and followed a tutorial to put page and post titles in the site tagline section. So what happens when I used your code is it just adds above content. I have been trying to figure out how to make the title of portfolio go to the tagline but cant figure it out. I am attaching my tagline code section.

    1. Brad Dalton Avatar
      Brad Dalton

      Don’t think i’ve written a tutorial covering that yet Ryan.

      Seems you want to add the portfolio CPT archive title in the tagline area.

      What you can do is add support for genesis-cpt-archives-settings to this parameter in the code.

      [code]
      ‘supports’ => array( ‘title’, ‘editor’, ‘author’, ‘comments’, ‘thumbnail’, ‘excerpt’, ‘trackbacks’, ‘custom-fields’, ‘revisions’, ‘page-attributes’, ‘genesis-seo’, ‘genesis-cpt-archives-settings’ ),
      [/code]

      Then you can filter the CPT archive title and return it to the tagline area.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.