3

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);
7

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.

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.