Jump to content

Body ID Based on URL - Wordpress/PHP


adirondak

Recommended Posts

I've been trying to implement the ID the Body Based on URL wordpress snippet.

 

First issue seems to be a syntax error

function curr_virtdir($echo=true){
        $url = explode('/',$_SERVER['REQUEST_URI']);
        $dir = $url[1] ? $url[1] : 'home'; // defaults to this if in the root
        $dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function
        if ($echo) echo $dir;
        return echo $dir; // ie. curr_virtdir(false)
}
function get_curr_virtdir(){
        curr_virtdir(false);
}

 

I Get a syntax error on this line below

return echo $dir; // ie. curr_virtdir(false)

 

If i remove this line completly the script seems to work for in the following situation

<body <?php id="<?php curr_virtdir(); ?>">

 

although I am trying to use the same functions as a conditional for my hardcoded navigation but it doesnt seem to work - any ideas?

<li><a href="http://www.url.com/" class="<?php if ( get_curr_virtdir() == "home" ){echo "navhomecurrent";} else {echo "navhome";}?>">Home<span></span></a></li>
                <li><a href="http://www.url.com/solutions/solutions/" class="<?php if ( get_curr_virtdir() == "solutions" ){echo 'navsolutionscurrent';} else {echo 'navsolutions';}?>">Solutions<span></span></a></li>
                <li><a href="http://www.url.com/about-us/about-us/" class="<?php if ( get_curr_virtdir() == "about-us" ){echo "navaboutuscurrent";} else {echo "navaboutus";}?>">About us<span></span></a></li>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.