Skip to content

Instantly share code, notes, and snippets.

@philipdowner
Created October 4, 2011 23:58
Show Gist options
  • Save philipdowner/1263202 to your computer and use it in GitHub Desktop.
Save philipdowner/1263202 to your computer and use it in GitHub Desktop.
Modify the title tag of a WordPress custom post type archive
<?php
add_filter('wp_title', 'archive_titles');
/**
* Modify <title> if on an archive page.
*
* @author Philip Downer <[email protected]>
* @link http://manifestbozeman.com
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version v1.0
*
* @param string $orig_title Original page title
* @return string New page title
*/
function archive_titles($orig_title) {
global $post;
$post_type = $post->post_type;
$types = array(
array( //Create an array for each post type you wish to control.
'post_type' => 'post_type_name_here', //Your custom post type name
'title' => 'Title tag text here' //The title tag you'd like displayed
),
);
if ( is_archive() ) { //FIRST CHECK IF IT'S AN ARCHIVE
//CHECK IF THE POST TYPE IS IN THE ARRAY
foreach ( $types as $k => $v) {
if ( in_array($post_type, $types[$k])) {
return $types[$k]['title'];
}
}
} else { //NOT AN ARCHIVE, RETURN THE ORIGINAL TITLE
return $orig_title;
}
}
?>
@eliasgomezsainz
Copy link

The code didn't worked for me. Could you help me?

Update: Just found the problem, I deactivated AIOSP, and now works! Let try to mix them together now. Thanks for your code!

@error500
Copy link

You don't have to deactivate AIOSP, just put a higher priority :
add_filter('wp_title', 'archive_titles',900);

@vinoddalvi
Copy link

wp_title filter is deprecated since WordPress 4.4 (see here). You can use document_title_parts instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment