I'm trying to check if the current URL equals the desired URL for making the link class active (Bootstrap).
Example
<?php
If ($currenturl = "show.php?name=123") {
?><li class="active">123</li><?php
}else{
?><li class="">123</li><?php
}
?>
Current URL -> $currenturl = $_SERVER['REQUEST_URI']
Now, I have this if I'm on show.php?name=456
Make sure you have the correct comparison inside the if. You're currently using the assignment operator:
Note the difference:
if($currenturl = "show.php?name=123") // assignment =
// changes $currenturl to "show.php?name=123"
// and then tests if("show.php?name=123") equivalent to if(true)
// see also http://stackoverflow.com/questions/5940626/evaluation-of-assignment-in-php
if($currenturl == "show.php?name=123") // comparison ==
Second: You have set $urlpage but comparing $currenturl. Use $urlpage
Code:
<?php $urlpage = $_SERVER['REQUEST_URI']; ?>
<?php if ($urlpage == "/show.php?nom=123") { ?>
<li class="active">123</li>
<?php } else { ?>
<li class="">123</li>
<?php } ?>
An alternative using a ternary operator:
<li <?php echo ($urlpage == "/show.php?nom=123") ? 'class="active"' : ''; ?>>123</li>
Applying to all pages:
<?php $pages = array('123', '456', '789'); ?>
<ul>
<li <?php echo (!isset($_GET['nom']) ? 'class="active"' : ''); ?>>Home</li>
<?php foreach($pages as $page): ?>
<?php if (isset($_GET['nom']) && $_GET['nom'] == $page) { ?>
<li class="active"><?php echo $page; ?></li>
<?php } else { ?>
<li class=""><?php echo $page; ?></li>
<?php } ?>
<?php endforeach; ?>
</ul>
An alternate way is to use the $_GET
if(isset($_GET['name']))
if($_GET['name'] == '123')
...
and so forth
Related
I'm attempting to add a for loop to a php menu that will add a tertiary level to a currently existing WP menu. Unfortunately, the only editable amount of content is that posted below, so please bear with me on this one.
I'm just wondering, before I attempt to implement this code, if anyone can tell me if it will actually work, of if you assume it'll break everything.
What I've done is to take the same semantic forloop that was written for the secondary (or 'sub_nav') menu items, and repeat it in attempts to dynamically generate third level (or 'tert_nav') menu items.
<div class="main-menu-sub-navigation">
<?php
$main_nav_items = wp_get_nav_menu_items(2);
$sub_nav = array();
foreach ($main_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$sub_nav[$parent_id][] = array('item' => $item);
}
}
$tert_nav = array();
foreach ($sub_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$tert_nav[$parent_id][] = array('item' => $item);
}
}
?>
<div class="sub-nav-inner">
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav sub-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($tert_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</div>
Any help would be so very greatly appreciated. Thank you!!
Here is my site nav.php is look like :
<li <?php echo $active; ?>>Home</li>
<li <?php echo $active; ?>>Report</li>
<li <?php echo $active; ?>>Chart</li>
<li <?php echo $active; ?>>Export</li>
and header.php page is look like :
<?php
$pageName = basename($_SERVER['PHP_SELF']);
$explode = explode('.', $pageName);
$title = ucfirst($explode[0]);
if($title == "Report") {
$active = 'class="active"';
} else {
$active = '';
}
?>
I am using above code to show css .active class on currently view any page. but it's adding .active class to all pages.
Note : I have 4 pages. e.g: index, report, chart and export.
and My full page structure is like that :
In index.php page I am calling following page.
header.php
nav.php
js.php
footer.php
use this
$activePage = basename($_SERVER['PHP_SELF'], ".php");
and in your li
<li <?php if($activePage=="Report") { echo "class='active'"; } ?>>Home</li></li>
Wrapping will give you additional flexibility over static content
$pageName = basename($_SERVER['PHP_SELF']);
$explode = explode('.', $pageName);
$title = ucfirst($explode[0]);
$nav = [
'index' => 'Home',
'report' => 'Report',
'chart' => 'Chart',
'export' => 'Export'
];
foreach ($nav as $key => $value) {
echo '<li class="'.($key == $title ? 'active' : '').'>';
echo ''.$value.'' ;
echo '</li>'
}
Easiest way:
$pages = array();
$pages["home.php"] = "Home";
$pages["report.php"] = "Report";
$pages["chart.php"] = "Chart";
$pages["export.php"] = "Export";
$active = "home.php";
<?php foreach($pages as $url=>$title):?>
<li>
<a <?php if($url === $active):?>class="active"<?php endif;?> href="<?php echo $url;?>">
<?php echo $title;?>
</a>
</li>
<?php endforeach; ?>
Because you're defining $active only once and for all at the same time.
You probably should work with an array:
$navigation = array('index', 'report', 'chart', 'export);
And now you loop through that array, checking, which is active:
foreach($navigation as $n) {
if(ucfirst($explode('.', $pageName) == $n) {
echo '<li class="active">' . ucfirst($n) . '</li>';
} else {
echo '<li>' . ucfirst($n) . '</li>';
}
}
Try to do it like this:
<li class="<?= ('Home' == $title) ? 'active' : ''; ?>">
Home
</li>
<li class="<?= ('Report' == $title) ? 'active' : ''; ?>">
Report
</li>
<li class="<?= ('Chart' == $title) ? 'active' : ''; ?>">
Chart
</li>
<li class="<?= ('Export' == $title) ? 'active' : ''; ?>">
Export
</li>
try doing the following
<li <?php echo ($pageName=="index.php")?" class='active'" :""; ?>>Home</li>
<li <?php echo ($pageName=="report.php")?" class='active'" :""; ?>>Report</li>
<li <?php echo ($pageName=="chart.php")?" class='active'" :""; ?>>Chart</li>
<li <?php echo ($pageName=="export.php")?" class='active'" :""; ?>>Export</li>
I have the below code which works perfectly. When the title is JohnPage, it does not display in the li.
<?php foreach($this->pages AS $page) { ?>
<li <?php echo $page['title'] == ('JohnPage') ? 'style="display:none";' : ''; ?>>
<a<?php echo ($page['active']?' class="active"':'');?> href="<?php echo $page['href'];?>"><?php echo $page['title'];?></a>
</li>
<?php } ?>
However, I want to say: if the title is either JohnPage or MyPage, do not display in the li. I tried the following:
<li <?php echo $page['title'] == ('JohnPage' OR 'MyPage') ? 'style="display:none";' : ''; ?>>
<li <?php echo $page['title'] == ('JohnPage' || 'MyPage') ? 'style="display:none";' : ''; ?>>
Neither works. Thanks for your help.
Instead of putting your conditional statements there. Try it this way.
<?php
foreach($this->pages AS $page) { ?>
<li <?php if($page['title'] == 'JohnPage' || $page['title'] == 'MyPage') { ?>style="display:none<?php } ?>">
<a <?php echo ($page['active']?' class="active"':'') ?> href="<?php echo $page['href'] ?>"><?php echo $page['title'] ?></a>
</li>
<?php
} // end foreach
?>
I have some php generated magento top-links with a screenshot below:
My goal is to separate the "Log In" link and have it floated to the left of the same row.
I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.
The code is below:
<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
<?php foreach($_links as $_link): ?>
<?php if ($_link instanceof Mage_Core_Block_Abstract):?>
<?php echo $_link->toHtml() ?>
<?php else: ?>
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
<?php endif;?>
<?php if (! $_link->getIsLast()):?>|<?php endif;?>
<?php endforeach; ?>
</ul>
Any ideas would be greatly appreciated!
CSS offers a way to style the last-child of a collection, no tinkering with PHP required.
http://tinker.io/926d2
ul.links.pull-right :last-child {
margin-left: 2em;
}
I answered a similar question not long ago. This adds the class "last-item" to the last item processed.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
Evening All,
Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.
If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.
public function getNavigation()
{
$links = $this->getLinks();
$linkArray = array();
$linkCount = count($links);
$i;
foreach($links as $link) {
if($i == $linkCount) {
$last = true;
} else {
$last = false;
}
$linkArray[] = 'link' => $link->getLink()->toHtml(),
'isLast' => $last
$i++;
}
return $linkArray();
}
Your block would then have minimal logic applied. Mainly just iterating through the result set.
Hope that makes sense, If not let me know and I will get you what you require.
I am building a CMS and I have problems with my navigation. I want to add the class active to the active item in the navigation. This would work fine if I had a page for every item, but I have other sections and links
Here is my code:
<div id="main-menu">
<?php foreach ($items as $item) : ?>
<?php $active = ''; ?>
<?php if (isset($page)) : ?>
<?php ($item->pageId === $page->id) ? $active = 'active' : $active = ''; ?>
<?php endif; ?>
<?php if ($item->link == '') : ?>
<?php echo $item->label; ?>
<?php else : ?>
<?php echo $item->label; ?>
<?php endif; ?>
<?php endforeach; ?>
When I do not have a page, then I give the link the value provided to point to the specific section. How can I make them all to work fine?
You don't need that many <?php ?> tags! Anyway, your problem seem to be an incorrect usage of the ternary operator. Try this:
<?php
foreach ($items as $item) :
$active = '';
if (isset($page)) :
$active = ($item->pageId === $page->id) ? 'active' : '';
endif;
if ($item->link == '') :
?>
<?php echo $item->label; ?>
<?php else : ?>
<?php echo $item->label; ?>
<?php
endif;
endforeach;
?>
Here's how to do it with only one pair of PHP tags:
<?php
foreach($items as $item)
{
$active = ($item->pageId == $page->id) ? 'active' : '';
if(!$item->link)
{
echo '' . $item->label . '';
}
else
{
echo '' . $item->label . '';
}
}
?>