PHP 导航菜单和活动页面样式 [英] PHP navigation menu and active page styling

查看:27
本文介绍了PHP 导航菜单和活动页面样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成我的网站导航/页面按钮并为活动页面提供样式元素.我目前所拥有的是将样式元素应用于主页,但是当导航到其他页面时,样式元素会保留在主页"按钮上,而不是将自身应用于当前页面.

Hi I'm trying to generate my websites navigation/page buttons and give the active page a style element. What I have at the moment is applying the style element to the home page but when navigating to other pages the style element stays on the 'Home' button rather than applying itself to the current page.

我的页面是动态的,网址如下:http://www.website.com/?p=联系

My pages are dynamic with the url being like so: http://www.website.com/?p=contact

<?php

    $current = array(
        "" => "Home",
        "?p=contact" => "Contact Us",
        "?p=about" => "About Us",
        "?p=privacy" => "Privacy Policy"
    );
    foreach( $current as $k => $v ) {
        $active = $_GET['page'] == $k
            ? ' class="current_page_item"'
            : '';
        echo '<li'. $active .'><a href="./'. $k .'">'. $v .'</a></li>';
    }

?>

我已经尝试了一些东西,但似乎无法正常工作,任何帮助将不胜感激.谢谢:)

I've tried a few things but can't seem to get it working correctly, any help would be greatly appreciated. Thank you :)

推荐答案

下面这行是罪魁祸首

$_GET['page'] == $k

$_GET['page'] == $k

您应该使用 $_GET['p'] 这是您的查询字符串参数.它将等于您的示例网址中的 contact.所以在没有 ?p=

You should use $_GET['p'] which is your querystring parameter. It will be equal to contact in your example url. So store your array values without the ?p=

<?php

    $current = array(
        "" => "Home",
        "contact" => "Contact Us",
        "about" => "About Us",
        "privacy" => "Privacy Policy"
    );
    foreach( $current as $k => $v ) {
        $active = $_GET['p'] == $k
            ? ' class="current_page_item"'
            : '';
        echo '<li'. $active .'><a href="./'. $k .'">'. $v .'</a></li>';
    }

?>

这篇关于PHP 导航菜单和活动页面样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆