[resolved] Extra spaces when echoing wp_title for body tag id (4 posts)

  1. Anyone want to take a guess at why, when I use this the below code to generate a body tag with the current page as the id (to have the current page menu item highlighted with CSS):

    <body id="<?php echo wp_title(''); ?>">

    I get this in the source, with the extra spaces in front of the title of the page:

    <body id=" Home">

    I think the extra spaces are not allowing my CSS to work.

    (I'm not using the wp_list_pages in my header as I have some custom php to generate page URLs, so I using current_page in my CSS won't work.

    Thanks, Mark

  2. Turns out one needs to use trim to clean up the leading spaces; might be a bug that allows the spaces through in the first place?

    <body id="<?php echo trim(wp_title('', false)); ?>">

  3. hardwork64
    Posted 6 years ago #

    Just noticed your post. Could you please explain how you got to target the current page in the menu?

  4. districtj
    Posted 5 years ago #

    Yep this totally fixed the same problem I had! Although for me it was more annoyance when I viewed my page source code, I could see that being an issue down the line when I get better with coding and need to assign it as a variable or something. And I was worried that it wouldn't transfer info correctly because I'm using the same code for Meta Tags, like such:

    <meta name="title" content="<?php echo trim(wp_title('', false)); ?><?php if (wp_title('', false)) { echo ' - '; } ?><?php bloginfo('name'); ?>" />

    By adding your snippet of code, the meta tags come out perfectly with no extra spaces.

    Thank you very much for finding that solution!

Topic Closed

This topic has been closed to new replies.

About this Topic