Skip to main content
added 128 characters in body
Source Link
Sumit
  • 4.9k
  • 2
  • 29
  • 36

The problem is global $post. As @Pieter suggested global $post is returning single post object (from main query) for menucan be alter anytime with some other plugin or code. Menu items are effected because global $post does not contain the correct object.

Instead of $post, the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2); 

The problem is global $post. $post is returning single post object (from main query) for menu items.

the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2); 

The problem is global $post. As @Pieter suggested global $post object can be alter anytime with some other plugin or code. Menu items are effected because global $post does not contain the correct object.

Instead of $post, the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2); 
deleted 39 characters in body
Source Link
Sumit
  • 4.9k
  • 2
  • 29
  • 36

The problem is global $post. $post always containis returning single post object (by default)from main query post so on single item you have main post ID even) for menu items.

the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2); 

The problem is global $post. $post always contain (by default) main query post so on single item you have main post ID even for menu items.

the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2); 

The problem is global $post. $post is returning single post object (from main query) for menu items.

the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2); 
Source Link
Sumit
  • 4.9k
  • 2
  • 29
  • 36

The problem is global $post. $post always contain (by default) main query post so on single item you have main post ID even for menu items.

the_title filter also provide you ID of current post in action, so use it in this way

function append_album_review_to_title( $title, $id ) { if ( get_post_type( $id ) == 'album_review' ){ return 'Album Review: ' . $title; } else { return $title; } } add_filter('the_title', 'append_album_review_to_title', 10, 2);