如何在PHP中使类别和当前页面突出显示?

发布于 2024-09-05 18:13:46 字数 456 浏览 2 评论 0原文

我试图找到一些关于使用 PHP 制作 CMS 类型类别的示例代码或最佳实践。

这个问题肯定已经被解决了无数次,但由于某种原因,我无法找到任何使用 PHP 来实现这个问题的示例代码。

据我所知,问题有两个部分。第一个与样式方面有关:

  • 在导航中输出链接,以便当前页面具有特殊样式(class =“active”)并且
  • 不打印当前页面的链接。

第二部分是处理类别、子类别以及类别下的动态页面。

第二部分看起来非常简单。我正在考虑使导航中的类别名称成为categories.php?id=x 的链接,并且在此页面上我只打印具有该类别id 的页面。然后,如果用户点击页面,他将被带到pages.php?id=y。

但是,我不太确定如何进行导航来检查我们现在是否位于当前页面。我应该使用一些大小写开关还是什么?

非常感谢任何想法或一些好的示例代码的链接。

I am trying to find some example code or best practices about making CMS-type categories with PHP.

This is a problem that has for sure been solved gazillion times but for some reason I am unable to find any example code using PHP about how to implement this.

As far as I can tell, there are two parts to the problem. The first one has to with the styling side of things:

  • outputting the link in the navigation so that the current page has a special style (class="active") and
  • to not print out the link for the current page.

The second part is handling categories, subcategories and the dynamic pages under the categories.

The second part seems pretty straightforward. I am thinking of making it so that the name of the category in the navigation is a link to categories.php?id=x and on this page I just print out the pages with that category id. Then, if the user klicks on a page he will be taken to pages.php?id=y.

However, I am not quite sure on how to make a navigation to check if we are now on the current page. Should I just use some case switch or what?

Any ideas or links to some good example code are much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佞臣 2024-09-12 18:13:46

如果您正在加载 CMS 样式的页面,那么您可能已经获得了某种可以在代码中访问的页面标识符? current_page_id 或者类似的东西?从那里我通常会这样做:

<ul id="menu"><?
foreach ($menu_items as $menu_item) {
    ?><li <?=($menu_item['page_id'] == $current_page_id) ? 'class="active"' : ''?>><a href="<?=$menu_item['link']?>"><?=$menu_item['title']?></a></li><?
}
?></ul>

If you're loading the page CMS-style presumably you've got some sort of page identifier that's accessible within the code? A current_page_id or something like that? From there I usually just do this:

<ul id="menu"><?
foreach ($menu_items as $menu_item) {
    ?><li <?=($menu_item['page_id'] == $current_page_id) ? 'class="active"' : ''?>><a href="<?=$menu_item['link']?>"><?=$menu_item['title']?></a></li><?
}
?></ul>
奈何桥上唱咆哮 2024-09-12 18:13:46

不需要 PHP,使用 CSS

a:active { color:#09f; }

UPDATE
使用这个小jquery代码

$("*").find("a[href='"+window.location.href+"']").each(function(){
   $(this).addClass("current");
   $(this).attr('href',"#"); //nullifying the link
   //add your own logic here if needed
})

no need for PHP, use CSS

a:active { color:#09f; }

UPDATE
use this little jquery code

$("*").find("a[href='"+window.location.href+"']").each(function(){
   $(this).addClass("current");
   $(this).attr('href',"#"); //nullifying the link
   //add your own logic here if needed
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文