• Resolved hellodrifter

    (@hellodrifter)


    Hello there,

    Complete wordpress/PHP neophyte here. I’m in the process of “translating” a static xhtml/css website into a wordpress theme, so please bear with me.

    Now, my nav menu highlighting relies on each page having its own unique ID within the body tag. Seeing as now I only have one body tag (in my header.php template), my nav menu highlighting is dead.

    I have 14 wordpress pages as it stands, and I’ve tried using the following PHP to dynamically spit out my body tag ID. I’m trying to use the page titles for the body tag ids.

    <?php if (is_front_page()) { ?>
    <body id="home">
    <?php } else { ?>
    <body id="<?php echo wp_title; ?>">
    <?php } ?>

    I repeat, I have no idea how to code in PHP. Can anyone tell me what I’ve done wrong in my code and how to go about fixing it? *_*

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter hellodrifter

    (@hellodrifter)

    Hey esmi,

    I want to thank you for taking the time to respond to my problem. Unfortunately, I think you’ve grossly overestimated my ability. ??

    I have read A LOT of the codex in my attempts to get familiar with wordpress. I’ve read the page about function body_class 3 times from start to finish before posting here. I have already tried to use body_class as my solution, but could not get it to work on my own.

    If you are still willing to help me, I will explain how you can. Please directly and specifically answer either of the two following questions:

    1) Please respond to my original question, which was what I have done wrong with the following code snippet, and how to go about fixing this code:

    <?php if (is_front_page()) { ?>
    <body id="home">
    <?php } else { ?>
    <body id="<?php echo wp_title; ?>">
    <?php } ?>

    2) If you absolutely think body_class is the best solution, please explain your alternative in detail. How does body_class dynamically create the body tag ID, how do I make it output custom ID tags, and how I can see what value is being output on a given wordpress page?

    Thread Starter hellodrifter

    (@hellodrifter)

    I found out what the problem was and the solution. Turns out another fellow was using a very similar method for creating body tag IDs.

    Problem one was syntax. I was missing brackets in the 4th line after wp_title.

    Problem two, for some reason, wordpress was inserting white space in my source before the body tag id. This calls for using something called trim to clean up the code.

    Here is the link to the solution.


    https://www.ads-software.com/support/topic/extra-spaces-when-echoing-wp_title-for-body-tag-id?replies=4

    I will leave the code, with corrections to line four, for anyone else who may use this same method for creating unique body tag IDs.

    <?php if (is_front_page()) { ?>
    <body id="home">
    <?php } else { ?>
    <body id="<?php echo trim(wp_title('', false)); ?>">
    <?php } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Broken CSS navigation calls for dynamic body ID’ is closed to new replies.