How to append text to title of Custom Post Type post (without affecting all titles on page)?

I believe checking in_the_loop will work for the main query:

function custom_title( $title ) {
    global $post;
    $text="Extra text: ";

    if ( get_post_type( $post->ID ) == 'custom_post_type' && in_the_loop() ){
        return $text . $title;
    }
    else {
        return $title;
    }
}

add_filter( 'the_title', array($this, 'custom_title' ) );

Leave a Comment