PHP get path and all subpaths (Drupal)
I have a hard-coded menu in Drupal (as it's too complex for the standard Menu system in Drupal).
I would like to be able to say: If this page is contained within the /about/ directory, apply the class "active", so that all new pages created within this directory automatically highlight the current section.
Currently I have:
$current_page = $_SERVER['REQUEST_URI'];
<ul class="main">
<li class="home"><a href="<?php echo $base_path?>">Home</a></li>
<li class="about
<?php if ($current_page == "/xxxxxxx.com/dev/about/")
{
echo "active";
}
?>"><a href="javascript:void(0)">About</a></li>
<li class="services"><a href="javascript:void(0)">Services</a></li>
<li class="work"><a href="javascript:void(0)">Work</a></li>
<li class="awards"><a href="javascript:void(0)">Awards</a></li>
<li class="e开发者_Python百科nvironment"><a href="javascript:void(0)">Environment</a></li>
<li class="contact"><a href="javascript:void(0)">Contact</a></li>
</ul>
I have tried a few variations of strpos and explode to get the right variable, but with no luck so far.
Thanks :)
I don't know anything about Drupal or your URL scheme, but the task of checking whether $current_page
contains "/about/" you can do with:
if (strpos($current_page, '/about') !== false) echo "active";
You should probably listen to googletorp though.
Try this function. It's like arg function, but parse real path.
function real_arg($index = NULL) {
$ofset = strlen(base_path());
$q = explode('?', substr($_SERVER['REQUEST_URI'], $ofset));
$q = explode('/', trim($q[0], '/'));
return isset($index) ? $q[$index] : $q;
}
In your case:
if(real_arg(0) == 'about') echo 'active';
Use the menu api then theme your links to match what you want. You won't need to duplicate functionality that already exists. You'll acquire a skill you can reuse.
See:
http://api.drupal.org/api/function/theme_menu_item/6
http://api.drupal.org/api/function/theme_menu_item_link/6
It shouldn't take long and you'll remove a layer of workarounds.
精彩评论