I aim to have a similar Bar at the top as here: http://www.keltainenporssi.fi/
The colour of the current site is in red, while other sites are in black.
How would you reach my aim?
Perhaps by php?
The syntax in pseudo-code may be
if $site = A
use-styleA
if $site = B
use-styleB
else
use-FinalStyle
It is common to use a server-side script such as PHP to do such a thing. For example:
<?PHP
echo '<ul id="top-nav">';
foreach($page as $pageId => $text) {
echo '<li';
if($curPage == $pageId)
echo ' class="active"';
echo '>';
echo '<a href="pages/' . htmlspecialchars($pageId) . '">';
echo htmlspecialchars($text);
echo '</a>';
echo '</li>';
}
echo '</ul>';
?>
This would produce output like: (Formatted for readability)
<ul id="top-nav">
<li>Home</li>
<li class="active">Page one</li>
<li>Page two</li>
</ul>
Non-dynamic approach:
You can use a CSS rule for each site, like so:
/* site-one.css */
#top-nav li.site-one a {
color: red;
}
/* site-two.css */
#top-nav li.site-two a {
color: red;
}
/* site-three.css */
#top-nav li.site-three a {
color: red;
}
HTML:
<ul id="top-nav">
<li class="page-one">Page one</li>
<li class="page-two">Page two</li>
<li class="page-three">Page three</li>
</ul>
You can have a single css for the bar which all the domains will be fetching. Make sure you add !important rules on that css so it's rules are not overriden by anything else. Otherwise shield the bar in a "namespace" by adding an id on the list/div or whatever you'll use to implement it.
Yes, you could assign the anchor element's class based on the current site.
Related
I know the title is kinda vague, sorry. I could'nt express it easily in one statement. So, anyways.
I'm currently working on my Personal Website. I wanted to have the nav element for the current page to be highlighted a certain color. To do so I wanted to use this:
<a href="index.php" class="nav-item" <?php if($current =='home'){echo 'class = "active"';}?>>Home</a>
and the same thing for the other pages. However, "class = "active" is not even being applied to the a tag. My Index pages contains this:
$current = "home";
and the css for the active class looks like this:
.active{
background-color: #fdfdfd !important;
color: #3D1c4B;
}
I seriously don't know what I'm doing wrong. Is there something I'm missing or is this just something I can't do with an a element?
Here is what the nav looks like
You can't have multiple class tags, you already have one you can't add a second. Try this:
Home
Your HTML is invalid, and would render: <a href="index.php" class="nav-item" class="active"> - which has two class attributes (not legal). Modify your PHP like below, to put both classes in the same attribute:
Home
You are writing class attribute two times in <a> tag
<a href="index.php" class="nav-item" <?php if($current =='home'){echo 'class = "active"';}?>>Home</a>
it should be like
<a href="index.php" class="nav-item <?php if($current =='home'){echo
' active'; }?>" >Home</a>
Get the class in a variable and use it like this:
<?php
$className = '';
if($current =='home'){
$className = 'active';
echo "<a href='index.php' class='nav-item " . $className . "'>Home</a>";
?>
First, I see your using:
<a href="index.php" class="nav-item" <?php if ( $current =='home' ) echo 'class = "active"'; ?> >Home</a>
is wrong, since you will get class="nav-item" class="active", - instead you need to write:
<a class="nav-item <?php if ( $current =='home') echo 'active'; ?>" href="index.php">Home</a>
your code will add 2 classes so that only one class will be applied to your tag.
try like this.
Home
I am using a PHP include that features my header content. For my navigation section I am using a list which just features text. I am using a small bit of php on the list items which basically states whatever the current page is, have an image displayed over that list item within the navigation. Below is what I am 'trying' to achieve:
As you can see, I want that blue shaped image placed over the top of the current page list item.
But below is what I have achieved and you will notice that the image is displayed but not like it should. I have used absolute positioning on the .current class which displays the whole image, however I cannot use absolute position on the image because then it wont display properly on another list item. What can I do?
Below is the relevant CSS and HTML/PHP:
<div id="navbar">
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="#">Home</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'shows.php')) echo 'class="current"';?> href="pages/shows.php">Shows</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'classes.php')) echo 'class="current"';?> href="pages/classes.php">Classes</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'noticeboard.php')) echo 'class="current"';?> href="pages/noticeboard.php">Notice Board</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="pages/about.php">Our Story</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="pages/contact.php">Contact</a></li>
</ul>
.current {
width:138px;
height:57px;
background-image: url('../images/current.png');
background-repeat: no-repeat;
}
the <li> element is restricting the size of your <a> tag.
set the size of the individual <li> items to match the size of your tag and you will see the entire thing.
Modified code:
<div id="navbar">
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="#">Home</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'shows.php')) echo 'class="current"';?> href="pages/shows.php">Shows</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'classes.php')) echo 'class="current"';?> href="pages/classes.php">Classes</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'noticeboard.php')) echo 'class="current"';?> href="pages/noticeboard.php">Notice Board</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="pages/about.php">Our Story</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="pages/contact.php">Contact</a></li>
</ul>
</div>
#navbar ul li {
width:138px;
height:57px;
}
.current {
width:138px;
height:57px;
background-image: url('../images/current.png');
background-repeat: no-repeat;
}
For obvious reasons, the background image of an element cannot overflow the element itself. Therefore, if you're putting the background image on your A tag, you need to make sure that the A tag is big enough to fit the background. You could try something like this:
A {
display: inline-block;
width: 138px;
height: 57px;
}
You'll have to adjust all your other CSS to position things properly.
I seem to run into this problem frequently and can never find a solution. I have a Wordpress site with a top navigation. Since this is in my header.php, and used on all pages, I cannot hardcode my active menu state for each page.
How can I dynamically set the activate state to work for each page?
Here is my current nav code:
<nav id="main-menu" class="padbot">
<ul id="ce">
<li class="cemenu">About</li>
<li class="cemenu">Consulting</li>
<li class="cemenu">Intelligence</li>
<li class="cemenu">Academy</li>
<li class="cemenu">Blog</li>
<li class="cemenu">Contact</li>
</ul>
I've already setup a CSS class called "active" that has my active state properties. Ideally, what I'm looking for is when your on the "About" page (or any of the other pages), the class I created for the active state will be appended to the current li classes's.
Example:
<li class="cemenu active">About</li>
Thanks!
you could try something along the lines of
<li class="cemenu<?php echo ($_SERVER['PHP_SELF'] == '/about' ? ' active' : '');?>">About</li>
You can do this way:
This will add the active class to the <a> which contains the page from the url.
$(function(){
var url = window.location.href;
var page = url.substr(url.lastIndexOf('/')+1);
$('.cemenu a[href*="'+page+'"]').addClass('active');
});
and if you want to add class to its parent li the replace the last line to this and css class should be like this:
.active a{
css
properties
for active li's a
}
// using closest
$('.cemenu a[href*="'+page+'"]').closest('li').addClass('active');
or
// using parent
$('.cemenu a[href*="'+page+'"]').parent('li').addClass('active');
just tryout the fiddle here
First, there is a css pseudo class prepared for styling 'active' links :
a:active {css}
For your situation, you would have to add this class to your styling :
.active a, a:active {css}
But your needs seems more on the PHP side than the CSS, perhaps someone else will help you with that part. There would be a javascript solution with jQuery, finding the actual location then inject a css selector to the proper element.
Check this article and this other one about wordpress. It will help you.
Stack Overflow references :
How do I target each menu different items on active state in Wordpress
How to add Active states and icons in wordpress wp_nav_menu()
Loosing Nav Active State in Wordpress Dynamic Menu
google search
try something like this:
<?php $pages = array('about' => 'About Us', 'blog' => 'blog') ?>
<ul>
<?php foreach($pages as $url => $page): ?>
<?php $isActive = $_SERVER["REQUEST_URI"] == $url ?>
<li<?php echo $isActive ? ' class="active"' : '' ?>>
<?php echo $page ?>
</li>
<?php endforeach ?>
</ul>
It may be worth looking into using wordpres functions such as get_page_link which would be nicer than using the Server super global as that's not nice. This would also fail if you have wordpress in a folder and not the document root, it's just a simple example to get you started :)
You can try like this
<li class="<?php
if($this_page=='Home'){
echo 'active';
}
?>">
Home
</li>
<li class="<?php
if($this_page=='Contact'){
echo 'active';
}
?>">
Contact
</li>
And then in your home page
$this_page='Home';
And in your contact page
$this_page='Contact';
You could use preg_replace() to add class="active" like this:
ob_start();
echo '<ul>
<li>Page 1</li>
<li>Page 2</li>
</ul>';
$output = ob_get_clean();
$pattern = '~<li><a href="'.$url.'">~';
$replacement = '<li class="active"><a href="'.$url.'">';
echo preg_replace($pattern, $replacement, $output);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I highlight a link based on the current page?
Context
I have been trying for ages to get a selected link in my Navigation menu to stay highlighted when clicked, but I cant find any decent understandable tutorials on this.
Questions
How can I resolve this?
Would jQuery or PHP be best for this?
Code
<ul>
<li>ABOUT US
<ul>
<li>Company</li>
<li>Team</li>
<li>Careers</li>
</ul>
</li>
<li>OUR WORK
<ul>
<li>Portfolio</li>
<li>Upcoming</li>
</ul>
</li>
<li>WHATS NEW
<ul>
<li>News</li>
<li>Blog</li>
</ul>
</li>
</ul>
Solution
The cleaner is to add a current class to selected link when you generate the page on the server side.
Example
I found this example which is close to your current architecture, according to the code that you gave us.
Source: http://www.finalwebsites.com/tutorials/css-navigation-bar.php
PHP
$items = array(
array('link'=>'scripts.html', 'label'=>'PHP scripts'),
array('link'=>'tutorials.html', 'label'=>'Tutorial'),
array('link'=>'template.html', 'label'=>'CSS template'),
array('link'=>'examples.html', 'label'=>'Code examples')
);
$menu = '<ul>';
foreach ($items as $val) {
$class = ($_SERVER['SCRIPT_NAME'] == $val['link']) ? ' class="current"' : '';
$menu .= sprintf('<li>%s</li>', $val['link'], $val['label']);
}
$menu .= '</ul>';
echo $menu;
CSS
ul li a.current { background-color: #40611F; color: #FFFFFF; }
echo $nav gives code like this:
<ul>
<li class="someclass">sometext
<ul>
<li class="someclass">sometext</li>
<li class="spacer"></li>
<li class="someclass">sometext</li>
<li class="spacer"></li>
<li class="someclass">sometext</li>
<li class="spacer"></li>
<li class="someclass">sometext</li>
<li class="spacer"></li>
</ul>
</li>
<li class="spacer"></li>
<li class="someclass">sometext</li>
<li class="spacer"></li>
</ul>
There are list items with class spacer inside each child ul, after each normal list item.
How do I remove the spacer list items which are grandchildren of the main list, using PHP?
Example: <ul> <li> <ul> <li class="spacer">
I'm searching for a regular expression, which should erase <li class="spacer"></li> only in a child <ul> element.
If you don't have access to the $nav variable to remove it (which you likely do) then I'd just use CSS to hide it, something like this should work:
li ul li.spacer {
display:none;
}
If however you have access to $nav - delete that spacer li from the code. Simples.
Also, on a side note. having empty elements like that on the page as "spacers" is semantically bad. This should be handled via CSS, add margins/padding on other elements on the page, don't use a class of spacer, if you do then you may as well go back to using stray <br /> tags everywhere to create spaces.
$xml = new SimpleXMLElement($nav);
$spacers = $xml->xpath('li//li[#class="spacer"]');
foreach($spacers as $i => $n) {
unset($spacers[$i][0]);
}
echo $xml->asXML();
This is converting to XML (use a recent PHP 5.3 version and DOMDocument to export to HTML). Output:
<?xml version="1.0"?>
<ul>
<li class="someclass">sometext
<ul>
<li class="someclass">sometext</li>
<li class="someclass">sometext</li>
<li class="someclass">sometext</li>
<li class="someclass">sometext</li>
</ul>
</li>
<li class="spacer"/>
<li class="someclass">sometext</li>
<li class="spacer"/>
</ul>
How about str_replace?
$nav = str_replace('<li class="spacer"></li>','',$nav);
edited code below
Based on the new requirement this code works. I know its hacky and sloppy but it works:
$temp = explode("\n",$nav);
for ($i=0;$i<count($temp);$i++) {
if (strstr($temp[$i],"<ul>")) {
$nested_ul = 1;
}
if (strstr($temp[$i],"</ul>")) {
$nested_ul = 0;
}
if ($nested_ul==0) {
if (!strstr($temp[$i],"spacer")) {
$new_nav .= $temp[$i]."\n";
}
} else {
$new_nav .= $temp[$i]."\n";
}
}
echo $new_nav;
"Easily" is relative. It depends on a few things. If you want, modify where the $nav is getting generated from.
use preg_replace to replace the li tags:
$new_nav = preg_replace('/<li class="spacer"></li>/', '', $nav);
echo $nav;
There are multiple ways:
Do not create it. It will be easier if you do not create something you do not want. It will be easier to maintain. So if you have any control over what is generated into $var string, just change it.
Simply replace it like that: str_replace('<li class="spacer"></li>', $var).
Use some HTML parser and remove the nodes.
Use JavaScript to remove <li class="spacer"></li> on client side.
Use substr_replace and strpos instead of str_replace, and specify an offset just after the first spacer.
http://www.php.net/manual/en/function.substr-replace.php
http://www.php.net/manual/en/function.strpos.php
Add the following CSS
ul ul li.spacer { display: none; }
Try this:
$nav = str_replace('<li class="spacer"></li>', '', $nav);