0

How can I add the custom post type to the title of the page, I mean the title in the tag in the header. It should look something like this: Webpage name | Products > Sony Playstation 4

Thanks ahead!

5

You can try this:

/**
 * Modify the header title on single pages 
 * but exclude the 'post', 'attachment' and 'page' post types. 
 * 
 * Header Title:  Site Name | Post Type Label | Post Title
 *
 * Example:       My Shopping Site | Products | Sony Playstation 4
 */
function wpse_141145_wp_title( $title, $sep ) {

    if( is_single() && 'post' !== ( $pt = get_post_type() ) )
    {
        // Get the current post type object's 'singular name' else 'name':
        $pto        = get_post_type_object( $pt );
        $pto_lsn    = $pto->labels->singular_name;
        $pto_ln     = $pto->labels->name;
        $pto_title  = ( ! empty( $pto_lsn )  ) ) ? $pto_lsn : $pto_ln ;

        // Space around the separator:
        $sep = sprintf( ' %s ', trim( $sep ) );

        // Construct the breadcrumb:    
        $title  = get_bloginfo( 'name', 'display' );
        $title .= $sep;
        $title .= $pto_title;
        $title .= $sep;
        $title .= single_post_title( '', FALSE );
    }
    return esc_attr( $title );
}
add_filter( 'wp_title', 'wpse_141145_wp_title', 99, 2 );

where we modify the header title on single pages, but exclude the post post type. We then recall that is_single() excludes the page and attachment post types.

Did you mean this kind of modification or did you have something else in mind?

3
  • It works good but i need the label because i use hebrew. How can i change it? – Samuel E. Apr 13 '14 at 12:10
  • Great, I updated the answer. Did you mean this kind of post type label name? – birgire Apr 13 '14 at 15:17
  • Nice answer, +1, Only a thing: probably you should esc_attr the title before returning it. – gmazzap Apr 13 '14 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.