the_title filter applying to menu items

I want to create a simple function to call the logged-in user’s username along with the word “Wishlist”.

It seems to be working but it also changes the menu item titles!

function customize_wishlist_title( $title ){
$current_user = wp_get_current_user();
$loggeduser = $current_user->user_login;
if ( is_page( 258 ) ) {
        $title="<span class="username">".$loggeduser.'\'s'.'</span>'.'&nbsp;'.'<span>Wishlist</span>';
    } 
return $title;
}
add_filter('the_title','customize_wishlist_title', 10, 2);

1 Answer
1

Use in_the_loop() conditional tag to make sure you are modifying the title inside the Loop and to prevent the hook changing titles somewhere else:

function customize_wishlist_title( $title ){
    $current_user = wp_get_current_user();
    $loggeduser = $current_user->user_login;
    // give attention to the following condition:
    if ( is_page( 258 ) && in_the_loop() ) {
        $title="<span class="username">".$loggeduser.'\'s'.'</span>'.'&nbsp;'.'<span>Wishlist</span>';
    } 
    return $title;
}
add_filter('the_title','customize_wishlist_title', 10, 2);

See in_the_loop() reference.

Leave a Comment