所以我选择了一个类,只有当你在你按下的链接的页面上时才能激活它,我试图用php中的代码获取它并询问当前页面是否等于任何一个链接和相同的一个链接应该得到类。
<?php if ($categories) { ?>
<div class="menu col-sm-5 noPadding">
<?php foreach ($categories as $category) {
$url = "http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]";
$isIt = $category['href']; ?>
<?php if ($url == $isIt){ ?>
<a class="selected" href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
这一切都在opencart中,我想继续使用foreach,而不是必须自己编写每个链接。 我试着回应它们,我得到完全相同的东西。
好的,这是var_dump($ categories)
的输出array (size=4)
'name' => string 'Desktops' (length=8)
'children' =>
array (size=0)
empty
'column' => string '1' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=20' (length=67)
1 =>
array (size=4)
'name' => string 'Components' (length=10)
'children' =>
array (size=0)
empty
'column' => string '1' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=25' (length=67)
2 =>
array (size=4)
'name' => string 'Cameras' (length=7)
'children' =>
array (size=0)
empty
'column' => string '1' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=33' (length=67)
3 =>
array (size=4)
'name' => string 'MP3 Players' (length=11)
'children' =>
array (size=0)
empty
'column' => string '4' (length=1)
'href' => string 'http://localhost/openC/index.php?route=product/category&path=34' (length=67)
答案 0 :(得分:0)
试试这个:
<?php if ($categories) {
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>
<div class="menu col-sm-5 noPadding">
<?php foreach ($categories as $category) { ?>
<?php if ($url == html_entity_decode($category['href'])){ ?>
<a class="selected" href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } else { ?>
<a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php } ?>
<?php } ?>
</div>
<?php } ?>