Support » Plugins » Extra spaces when echoing wp_title for body tag id

  • ResolvedModerator Mark Ratledge


    Forum Moderator

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Mark Ratledge


    Forum Moderator

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

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

    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Extra spaces when echoing wp_title for body tag id’ is closed to new replies.