1

I know – at first glance the problem may seem easy to solve – and I hope it is – I just can't figure out where/ how to check if I'm really on a single page, or just rendering a single page in the loop.

I basically need to alter the page title of CPT pages. This is what i have tried so far:

function event_page_title($title) {

    global $post;

    if ( ( 'event' == $post->post_type ) && ( is_singular() ) ) {

    $title = "Title: " . $title; 

    return $title;

}

add_filter('the_title', 'event_page_title', 0);

Problem is: the title is also altered in listings (WP_query) – although not in archive pages. I hope somebody can point me some direction? Thank you!

1 Answer 1

4

You can use is_singular() with post types like this:

if( is_singular('event') ) {
   // We are in single view of event post type
}

So, your code could be:

add_filter( 'the_title', 'event_page_title' );
function event_page_title( $title ) {

    if ( is_singular( 'event' ) ) {

        $title = "Title: " . $title;
    }

    return $title;

}
1
  • Thank you very much! I obviously had a misconception about how is_singular() works – I tried is_singular() before, but without the post type. Now it works like charm. Mar 2, 2016 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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