I need the "Pages" link to be able to detect if the user is on any of the pages that are included in the dropdown menu for "Pages". Detecting whether they are on only 1 page is easy, I need to add multiple instances to the "Pages" item. Hopefully that makes a little more sense.
Here is what I have:
<li class='dropdown' id="menu1">
<a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">Pages</a>
<ul class="dropdown-menu">
<li>
Home
</li>
<li>
About
</li>
</ul>
</li>
A example of what the code would look like for JUST index.php would be:
<li <?php if (stripos($_SERVER['REQUEST_URI'],'index.php') !== false) {echo 'class="active"';} ?>>
Home
</li>
Like I said before, I need to add multiple instances in basically one PHP block to detect MORE THAN if the user is JUST ON [filename].php
Hopefully this makes sense.
First you need to extract just the page name, then you need to compare it to the array of posible pages:
<?php if (in_array(preg_replace('|.*/(.+\.php).*|i','\\1',$_SERVER['REQUEST_URI']), array('index.php','about.php'))) {echo 'class="active"';}
You'll have to put your list items in an array. And then loop over them to create the menu.
$pages = array(
'Home' => 'home.php',
'About' => 'about.php'
);
<?php foreach ($pages as $name => $url): ?>
<li <?php if (stripos($_SERVER['REQUEST_URI'], $url) !== false):
echo 'class="active"';
endif ?>>
<?php echo $name?>
</li>
<?php endforeach ?>
This will check each result in the array to see if it matches the uri. If it does it will add the active class to the li.
Related
i am trying to highlight the page i am in this is what i have so far
<ul class="navigation list-unstyled components">
<li class="active">
Home
</li>
<li>
Contacts
</li>
<li>
aboutus
</li>
<li>
aboutyou
</li>
</ul>
Here is the only jquery I have tried so far, but it doesn't seem to work:
$(".navigation a").on("click", function () {
$(".navigation ").find(".active").removeClass("active");
$(this).parent().addClass("active");
});
If you're building a standard WordPress website, then your pages will reload when you click a navigation link, so even if you set the class with jQuery it will be reset on page load.
What you should do is to set the class of the navigation elements in PHP.
This code is just a mockup, please adjust it to fit your needs:
$home = is_front_page();
# Checks if it's the site front page
$contacts = is_page('contacts');
# Checks if the current page is a page called 'contacts'
$aboutus = is_page('aboutus');
# Checks if the current page is a page called 'aboutus'
$aboutyou = is_page('aboutyou');
# Checks if the current page is a page called 'aboutyou'
<ul class="navigation list-unstyled components">
<li <?php if ($home) {echo 'class="active"';} ?>>
Home
</li>
<li <?php if ($contacts) {echo 'class="active"';} ?>>
Contacts
</li>
<li <?php if ($aboutus) {echo 'class="active"';} ?>>
aboutus
</li>
<li <?php if ($aboutyou) {echo 'class="active"';} ?>>
aboutyou
</li>
</ul>
I've been making a website for a client that is based off of 1 page and the links are to different categories or articles. I'm showing the content depending on the URL parameter like this,
<a href="index.php?cat_id=<?php echo $category['cat_id']" >
usually to do a navigation highlight depending on the page I would do something like this,
PHP
<ul>
<li <?php if($pagename == "index.php"){ echo 'class="selected"'; } ?>>
Home
</li>
<li <?php if($pagename == "about.php"){ echo 'class="selected"'; } ?>>
About
</li>
<li <?php if($pagename == "services.php"){ echo 'class="selected"'; } ?>>
Services
</li>
<ul>
but my URLS are dynamic from the database like so,
PHP
<ul>
<li>Home</li>
<?php while ($category = $statement->fetch()) { ?>
<li><?php echo $category['cat_name']; ?></li>
<?php
}
?>
</ul>
So I was wondering how can I add the class selected to the links cat_id when the URL contains that parameters id? Thanks in advance for any help!
If the category IDs do not change over time:
if($_GET['category_id'] == "1" ){ echo 'class="selected"'; }
If the categorie IDs are subject to change over time, you will need to perform a pre-query to get a map of category IDs to pages. Then you can do something like:
if($_GET['category_id'] == $categories['home_page'] ){ echo 'class="selected"'; }
<?php if($category['cat_id']==$_GET['cat_id']){ echo 'class="selected"';}?>
<ul class="main-nav">
<li class="menu-item <?php if( is_category('one') || in_category('one') ) echo 'current_cat'; ?>">
one
</li>
<li class="menu-item <?php if( is_category('two') || in_category('two') ) echo 'current_cat'; ?>">
two
</li>
<li class="menu-item <?php if( is_category('three') || in_category('three') ) echo 'current_cat'; ?>">
three
</li>
<li class="menu-item <?php if( is_category('four') || in_category('four') ) echo 'current_cat'; ?>">
four
</li>
<li class="menu-item <?php if( is_category('five') || in_category('five') ) echo 'current_cat'; ?>">
five
</li>
</ul>
The bottom line is that on the main page (index.php) is allocated category (in fact in theory this should not happen), a little closer examination I realized that the category in the main determined by the first post on a page - in which category he belongs. Suppose on the main page is present the latest news from the category of "2" - then the index page is the category of "2" ... What is the problem and how to fix it is not quite clear to me the behavior? What features are not taken into account? Thank you for your future answers.
And I ask forgiveness for broken English.
First of all, make sure that code is not running inside the loop, that might be the key problem. If this is happening even outside the loop, you can avoid it to be triggered on the home page by modifying your conditions like this:
if( is_category('one') || (in_category('one') and is_single()) )
If I understand what you are asking, you don't want this code to trigger on the home page?
to make sure this is only run on individual post pages put this all between:
<?php if(is_single(): ?>
<!-- your code-->
<?php endif; ?>
is_single
stll I'm not sure what you are asking.
I have a magento website with multi store and i have been able to add the home link on the default store. the whole site along with its multi stores shares the same theme but in the theme i edited template/page/navigation/top.php with the code
<?php
$_anyActive = false;
foreach ($this->getStoreCategories() as $_category)
{
$_anyActive = $_anyActive || $this->isCategoryActive($_category);
}
?>
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>">
<span><?php echo $this->__('Home') ?></span>
</li>
This code now makes the home link shows but now its only shows in the default store but i want it to show in all other stores, i dont understand this but as they whole stores shares the same theme, i thought maybe they should also be able to read this code and display the home link, i could duplicate the themes and then assign them to each store (which i'm not even sure would work) but that would make the code deficult to maintain as i though if they share the same design, i could just make one change and reflect on the entire sub stores.
The following is the content of template/page/navigation/top.php
<?php $_menu = ''?>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>
<?php if ($_menu): ?>
<div class="nav-container">
<ul id="nav">
<?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><span><?php echo $this->__('Home') ?></span></li>
<?php echo $_menu; ?>
</ul>
</div>
<?php endif; ?>
I hope you are able to help me solve this problem
Use following code
<li class="home <?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()):?> active<?php endif;?>"><span><?php echo $this->__('Home') ?></span></li>
I guess the problem is that you put your home link inside of main navigation. As your secondary stores don't have any navigation, the whole unordered list has not been generated.
Try this:
<?php
$_menu = '';
$_anyActive = false;
foreach ($this->getStoreCategories() as $_category){
$_menu .= $this->drawItem($_category);
$_anyActive = $_anyActive || $this->isCategoryActive($_category);
}
?>
<div class="nav-container">
<ul id="nav">
<li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><span><?php echo $this->__('Home') ?></span></li>
<?php echo $_menu; ?>
</ul>
</div>
Neeraj Garg's answer was the solution in the end, but I wanted to expand on it.
Go to /app/design/frontend/default/yourtheme/template/page/html/topmenu.phtml
That might be specific to my theme. I think most would be using top.phtml in a different folder, as mentioned in the question. Turn on Template Path Hints in the System->Configuration->Advanced->Developer section (you may need to change your scope) to find out what file your navigation menu can be edited in. Make sure you copy and paste it outside of your base theme into your current theme, if it is using the base theme.
After that, I used a similar solution to Neeraj's suggestion. Mine looks like this, and is obviously dependent on the theme I am using (yours will likely look much different, as it will likely need to be catered to your theme).
<li class="level0 nav-0 first level-top <?php if (Mage::helper('core/url')->getCurrentUrl() === Mage::helper('core/url')->getHomeUrl()):?> active<?php endif;?>">
<a href="\" class="level-top">
<span>Home</span>
</a>
</li>
<?php echo $_menu ?>
I'm not entirely sure if this is even possible, but I'll try my best to explain my dilemma.
A website I'm working on has a header that contains links to important pages. The header html code resides in one file, which is included (php include) in all other pages.
This header.html consists of this code:
<li class="m1">Home</li>
<li class="m2">FAQs</li>
<li class="m3">Status</li>
<li class="m4">Contact Us</li>
The page currently open is meant to have its class set to "active", but this doesn't work since my header code resides in just one file.
Is there any way, using php or something else, that I can set the appropriate link to class "active" depending on which page is opened?
So, if I click on FAQ, I want the code to look like this:
<li class="m1">Home</li>
<li class="m2">FAQs</li>
<li class="m3">Status</li>
<li class="m4">Contact Us</li>
Yes you quite easily check which page you're on using $_SERVER['PHP_SELF'].
A good way to approach this would be to store your links/titles in an array, then loop through them and echo the active class when it equals PHP_SELF:
<?php
$links = array('Home' => 'home.php', 'FAQs' => 'faq.php', 'Status' => '', 'Contact Us' => '');
foreach ($links as $text => $page)
{
echo '<li><a href="'. $page .'"';
if ($_SERVER['PHP_SELF'] == $page)
{
echo ' class="active"';
}
echo '>'. $text .'<a></li>';
?>
Here's another route you can take:
<?php
function getCurrPage() {
$org_page = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
$stripped_page = substr($org_page, 0, -4);
return $stripped_page;
}
$curPage = getCurrPage();
$actPage = array();
$actPage[$curPage] = ' class="active"';
?>
<li class="m1"><a href="home.php"<?=$actPage['home'];?>>Home</a></li>
<li class="m2"><a href="faq.php"<?=$actPage['faq'];?>>FAQs</a></li>
<li class="m3"><a href=""<?=$actPage['status'];?>>Status</a></li>
<li class="m4"><a href=""<?=$actPage['contact'];?>>Contact Us</a></li>
You can use JQuery (JavaScript) to do it dynamically:
$(document).ready(function() {
//Add the current class to all links pointing to the current page
search = window.location.search;
$("*").find("a[href='" + search + "']").each(function(){
$(this).parent('li').addClass("active");
//add your own logic here if needed
})
})
Which works quite well.
The other alternative is to use $_SERVER['PHP_SELF'] or __FILE__:
<li class="m1"><a href="home.php"<?php if (basename($_SERVER['PHP_SELF']) == 'home.php'): ?> class="active"<?php endif; ?>>Home</a></li>
<li class="m1"><a href="faq.php"<?php if (basename($_SERVER['PHP_SELF']) == 'faq.php'): ?> class="active"<?php endif; ?>>FAQ</a></li>
<li class="m1"><a href="status.php"<?php if (basename($_SERVER['PHP_SELF']) == 'status.php'): ?> class="active"<?php endif; ?>>Statys</a></li>
<li class="m1"><a href="contact_us.php"<?php if (basename($_SERVER['PHP_SELF']) == 'contact_us.php'): ?> class="active"<?php endif; ?>>Contact Us</a></li>