Skip to content

Instantly share code, notes, and snippets.

@jonbrockett
Created January 17, 2019 14:23
Show Gist options
  • Save jonbrockett/0f21f7d0645ae1208c0bc6c18f63c1df to your computer and use it in GitHub Desktop.
Save jonbrockett/0f21f7d0645ae1208c0bc6c18f63c1df to your computer and use it in GitHub Desktop.
CPT Archive Slug to Title
<?php
/**
* Archive Slug to Title
*
* Gets `this` post's post type, then gets the archive slug of the post
* then outputs the slug, replacing `-` with ` ` and uppercasing every word
*
* @NOTE: This is not a perfect solution but will work for 90% of use cases.
* For more control, create an ACF `options` page of that post type with a
* text input. Then use this for the title. If doing this, it's also a
* good idea to add a notice for that archive explaining where to edit the
* page title. AVOID manually adding the title to the template.
*/
// Vars
$cpt_obj = get_post_type_object($post->post_type);
$cpt_obj_archive_slug = $cpt_obj->has_archive;
$cpt_obj_archive_title = ucwords(str_replace('-', ' ', $cpt_obj_archive_slug));
?>
<h1 class="entry-title"><?php echo $cpt_obj_archive_title; ?></h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment