I am making a portfolio website. In the menu I have the follwing itmes:
Home, Services, Portfolio(dropdown), About me, Contact.
Under projects I have to following folder structure:
Website root -> Portfolio -> projectName.html
When a menu item is active it has a blue background. As it is now, it is only html, so I have assign the correct header, everytime I make a new page there has to be under fx Portfolio. To summary up. I have 5 different headers, where each header have the specific choosen menu blue background attached.
I would like to use 1 header where the function is: If I click on "About me" the "active class" is called, which means the blue background is active. If I click projectName.html under Portfolio the Portfolio menu is active with a blue background.
The only way I know how to make that work is if there exist a user log in, and a session. Can I do something with CSS like this?
<!-- begin nav -->
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span>Tap me!</span>
</button>
<!-- begin logo in navigation -->
<a class="navbar-brand" href="index.php">
<img src="images/webdesign_Logo.png">
</a>
<!-- end logo in navigation -->
</div>
<div class="navbar-collapse collapse">
<ul class="nav nav-pills nav-main pull-right">
<!-- begin navigation items -->
<li class="active">Home</li>
<li>
Services
</li>
<li class="dropdown">
Portfolio
<ul class="dropdown-menu">
<li>
Portfolio 2 Column
</li>
<li>
Portfolio 3 Column
</li>
<li>
Portfolio 4 Column
</li>
<li>
Portfolio Single Item
</li>
<li>
Portfolio Single Full Width
</li>
<li>
Portfolio Single Options
</li>
<li>
Blog
</li>
</ul>
</li>
<li>
About Me
</li>
<li>Contact</li>
<!-- end navigation items -->
</ul>
</div>
</div>
</div>
</div>
</nav>
<!-- end nav -->
You can use css to perform that effect of a blue background when active:
yourDivId li:active{
background-color: blue;
}
yourDivId li:hover{
background-color: lightblue;
}
Using that css you can use JS to change the class to "active" when each li is clicked.
You can see what I mean here:
http://www.w3schools.com/cssref/sel_active.asp
Read this:
Tip: The :active selector can be used on all elements, not only links.
Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :hover selector to style links when you mouse over them.
Note: :active MUST come after :hover (if present) in the CSS definition in order to be effective!
About the JavaScript part you can use this answer on this forum:
$('li a').click(function(e) {
e.preventDefault();
$('a').removeClass('active');
$(this).addClass('active');
});
From: Add & remove active class from a navigation link
first, i suggest create your nav in separate php file and include it in other file and.
second, use javascript code like this:
<script>
document.getElementById("**yourMenuItems**").children.item(<?php echo $SelectedMenu; ?>).classList.add("active");
</script>
thired, when include your nav file set $SelectedMenu variable.
I suggest to use PHP and this Class to get a basic template system and it also includes a function to add "active" class to menus.
<?php
/**
HOW TO USE THIS CLASS.
In every page:
-----------------------------------------------------------------------------------------------
<?php
require 'Page.php'
$page = new Page('Home', 1); <-- initialize the page object
$page->addJS('path/tojs.js'); <-- add javascripts if needed
$page->addCSS('path/tocss.css') <-- add css if needed
$page->startBody(); <--- start the page body
?>
--HTML HERE--
<?php
$page->endBody(); <--- close the body
echo $page->render('inc/template.php'); <-- render the template
------------------------------------------------------------------------------------------------
**/
class Page {
private $title, $id = 0, $stylesheets=array(), $javascripts=array(), $body;
function __construct($title, $id = 0) {
$this->title = $title;
$this->id = (int)$id;
}
function setTitle($title) {
$this->title = $title;
}
function setId($id){
$this->id = (int)$id;
}
function addCSS($path) {
$this->stylesheets[] = $path;
}
function addJS($path) {
$this->javascripts[] = $path;
}
function startBody() {
ob_start();
}
function endBody() {
$this->body = ob_get_clean();
}
function render($path) {
ob_start();
require($path);
return ob_get_clean();
}
function activeMenu($id){
echo $this->id == $id ? 'active' : '' ;
}
function minifyCSS(){
$buffer = "";
foreach ($this->stylesheets as $css) {
$buffer .= file_get_contents($css);
}
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
return $buffer;
}
}
Then in the template:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $this->title; ?></title>
<?php
foreach ($this->stylesheets as $stylesheet) {
echo '<link href="' . $stylesheet . '" rel="stylesheet" type="text/css" />' . PHP_EOL;
}
//or echo '<style>'.$this->minifyCSS().'</style>'; to get minimized css
?>
</head>
<body>
<nav>
Home
Other Page
</nav>
<?php echo $this->body; ?>
<?php
foreach ($this->javascripts as $javascript) {
echo '<script src="' . $javascript . '"></script>' . PHP_EOL;
}
?>
</body>
</html>
Related
I have a problem with highlighting using php include.
Here's my navigation bar:
<?php
echo '
<link rel="stylesheet" href="css/index-css.css" type="text/css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/womsy/index.php">Womsy</a>
</div>
<ul class="nav navbar-nav">
<li <?php echo $active[1] ?>Home</li>
<li <?php echo $active[2] ?>Blog</li>
<li <?php echo $active[3] ?>Projects</li>
<li <?php echo $active[4] ?>Contact</li>
<li class="active">About</li>
</ul>
</div>
</nav>';
$active[$current] = "class=active";
?>
I made this with bootstrap, and am trying to highlight via this: http://webdevjunk.com/coding/css/17/php-menu-includes-with-css-style-to-highlight-active-page-link/
<?php
$current = 1;
include 'php/menu.php';
?>
This is what I use on the page, but it doesn't highlight, but when I change the class in the <li> it does work.
Can someone help me figure this out?
function setActive($pageName)
{
if (stripos($_SERVER['REQUEST_URI'], $pageName)) {
echo 'class="active"';
}
}
//$pageName will be home ,blog,projects and contacts
Declare page variable at the very top of each individual page.
Example: <? $page="home";?> goes at top of homepage and <? $page="blog";?> would go on top of blog page, etc.
For each list item in your menu add if statement with corresponding variable and active class.
Example: <ul><li class="<?if($page=="home"){?>active<?}?>"><b>Home</b></li><li class="<?if($page=="blog"){?>active<?}?>"><b>Blog</b></li></ul>
What this does is assign each page a variable. The variable is compared in the if statements in the nav bar to determine which li gets the active class.
my goal is to tweak/replace the existing wordpress php menu code, having as a start point, this html/css/js menu structure in codepen:
Home / Portfolio / About and going down in Portfolio structure, another page having Home / Portfolio projects.
In a few words, I've registered in script-calls.php the mynewmenu.js file with the following sequence
js:
// mynewmenu implementation
jQuery(function($){
var height, index, prevIndex
$('nav ul li').mouseover(function(e){
//Set the aesthetics (similar to :hover)
$('nav ul li').removeClass('hovered');
$(this).addClass('hovered');
//Gets relevant data required for scrolling of menuRight
height = $("#menuRight").css("height").replace('px','');
index = $(this).index();
//If the category button (from the navlist) has changed
if (index != prevIndex){
$("#menuRight").stop();
$("#menuRight").animate({"scrollTop":height*index}, 800, 'easeOutBounce'); //This requires jQuery UI for easing options.
prevIndex = index;
}
});
});
I've created the desired menu structure (assigning different menus to different pages as u'll see here) and I've identified the php that manage the existing menu structure:
<!-- Start Header -->
...
<div class="myrow max_width ">
<div class="small-5 <?php if ($header_style == 'style2') { echo 'medium-8'; } else { echo 'medium-4';} ?> columns menu-holder">
<?php $full_menu_true = ($menu_mobile_toggle_view == 'style2' && $header_style == 'style2');?>
<?php if ($full_menu_true) { ?>
<nav id="full-menu" role="navigation">
<?php if ($page_menu) { ?>
<?php wp_nav_menu( array( 'menu' => $page_menu, 'depth' => 2, 'container' => false, 'menu_class' => 'full-menu', 'walker' => new thb_mobileDropdown ) ); ?>
<?php } else if(has_nav_menu('nav-menu')) { ?>
<?php wp_nav_menu( array( 'theme_location' => 'nav-menu', 'depth' => 2, 'container' => false, 'menu_class' => 'full-menu', 'walker' => new thb_mobileDropdown ) ); ?>
<?php } else { ?>
<ul class="full-menu">
<li>Please assign a menu from Appearance -> Menus</li>
</ul>
<?php } ?>
/* I think that <div id='menuRight'> html sequences *translated*
in *php* should begin here in a conditional manner: *if* we find ourself on the
Home page, should be activated Home / Portfolio / About sequence and also if we
are on the Portfolio page, we should receive the menu 2, generated by Home / Portfolio
projects sequence. More details below. */
</nav>
<?php } ?>
<?php if ($header_search != 'off') { do_action( 'thb_quick_search' ); } ?>
<?php if ($header_cart != 'off') { do_action( 'thb_quick_cart' ); } ?>
<a href="#" data-target="open-menu" class="mobile-toggle<?php if (!$full_menu_true) { ?> always<?php } ?>">
<div>
<span></span><span></span><span></span>
</div>
</a>
</div>
</div>
</header>
<!-- End Header -->
At this point, my question is, how can I implement into the header.php the following html sequences that generate the rollover images linked to the menu buttons, keeping in mind that there are different sections, each menu with his section as follow. Home / Portfolio / About:
<nav>
<ul>
...
</ul>
<div id='menuRight'>
<div>
Home
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
<div>
Portfolio
<img src='http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'>
</img>
</div>
<div>
About
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
</div>
</nav>
and menu 2, Home / Portfolio projects:
<nav>
<ul>
...
</ul>
<div id='menuRight'>
<div>
Home
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
<div>
Fiva
<img src='http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'>
</img>
</div>
<div>
Caterham
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
<div>
Mobile
<img src='http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'>
</img>
</div>
</div>
</nav>
I left intentionally the css out of discussion because, that's another chapter of this code tweak.
LE: I have to mention that the rollover image effect activated by the menu buttons, will be enough (and make sense) for it to be available, only on the Home page and Portfolio page of the site. I think that It could be very tricky to achieve the same effect when we have a project page opened (let's say FIVA) and have the mouse over another project button for example.
LE2: regarding the rollover images, I am not looking for a fancy php code that's supposed to grab a preview of the last project or something; a php code that allows me to predefine image source links like we have in the above html menu sections, will be just fine, taking in consideration the fact that these images will not be replaced so often.
LE3: Pure experimental, and please correct me, I was just thinking, using include PHP function to call two separate html files in header.php (including in these the above described menu 1 and 2 sections) could be the beginning of a workaround?
<!-- Start Header -->
...
<div class="myrow max_width ">
...
<ul class="full-menu">
<li>Please assign a menu from Appearance -> Menus</li>
</ul>
<?php } ?>
<?php
/* But there still should be a php code, that call the
html sections *if* we are on Homepage or Portfolio page.
Something like:
Php instructions, if Home page */
include('menu1section.html');
// and also php instructions, if Portfolio page
include('menu2section.html');
?>
</nav>
<?php } ?>
<?php if ($header_search != 'off') { do_action( 'thb_quick_search' ); } ?>
...
</div>
</header>
<!-- End Header -->
Any thoughts? Thank you,
I'm not a wordpress dev, but it seems to me you need a way to detect the active category/post/page? you are on... it's difficult to understand your data structures from looking at the website.
<!-- Start Header -->
...
<div class="myrow max_width ">
...
<ul class="full-menu">
<li><a href="<?php echo get_admin_url().'nav-menus.php'; ?>">
Please assign a menu from Appearance -> Menus
</a></li>
</ul>
<?php } ?>
<?php
$thisCat = get_category(get_query_var('cat'));
switch ($thisCat->name) {
case 'Home':
echo "Home test";
break;
case 'Portfolio':
echo "portfolio test";
break;
case 'About Us':
echo "About Us test";
break;
}
?>
....
<!-- End Header -->
Revised my answer to give you a better idea of what test I'm referring too.
This might be very easy for some but I've tried various ways to get it to work but to no avail.
Here's a brief;
I have one page, I have links in this page that when clicked load other pages but display them in the same page.
<section class="posts-header">
<div class="col span-3-of-3">
<ul class="posts-nav" id="navigation">
<li>View Posts</li>
<li>View Posts</li>
<li>Create Post</li>
</ul>
</div>
</section>
The above code shows links that when clicked, pass a variable using $_GET to the switch statement below;
if(isset($_GET['source'])) {
$source = $_GET['source'];}
switch($source) {
case 'view_posts';
include "php/blog_posts_view.php";
break;
case 'view_all_posts';
include "php/blog_posts_reviewer.php";
break;
case 'add_posts';
include "php/blog_posts_addform.php";
break;
case 'edit_posts';
include "php/blog_posts_editform.php";
break;
default:
include "php/blog_posts_view.php";
break;}
How can I highlight the current page? using html, js, css3 or all.
Thanks.
You can just define an active class in CSS, which describes the style of an active link. For example, a very simple style:
.active {
color: red;
}
Then you have to modify your menu:
<section class="posts-header">
<div class="col span-3-of-3">
<ul class="posts-nav" id="navigation">
<li><a href="blog_admin_posts.php?source=view_all_posts"<?=$_GET['source'] == "view_all_posts" ? " class=\"active\"" : ""; ?>>View Posts</a></li>
<li><a href="blog_admin_posts.php?source=view_posts"<?=$_GET['source'] == "view_posts" ? " class=\"active\"" : ""; ?>>View Posts</a></li>
<li><a href="blog_admin_posts.php?source=add_posts"<?=$_GET['source'] == "add_posts" ? " class=\"active\"" : ""; ?>>Create Post</a></li>
</ul>
</div>
</section>
The active menu link text should now be displayed in red.
maybe you will find this very helpful, it adds your class to the current page and allows to add more links to your page by simply adding to the array.
<?php
$page = isset($_GET['page']) ? $_GET['page']: ' ';
$pages = ['view_all_posts','view_posts','add_posts'];
$active = '';
if (in_array($page,$pages)){
$active = 'active';
}
?>
Then modify your menu:
<section class="posts-header">
<div class="col span-3-of-3">
<ul class="posts-nav" id="navigation">
<?php foreach ($pages as $key => $value) : ?>
<li>
<?=$value?>
</li>
<?php endforeach;?>
</ul>
</div>
</section>
You can get the current page name using this:
$pageName = $_GET["source"];
And then, for every list item, you could do something like this:
<li class="<?= ($pageName === 'firstPageName') ? 'active' : '' ?>">...</li>
Of course, you'll need to style the class .active or how ever you would like to name it.
I'm still new to Front End Development and working on my first really big site / wordpress blog.
So my question is this, I have a Menu that will be the same on all pages on the site. However each section will have it's name highlighted in the Menu Nav.
Current Nav_Bar markup:
<div id="nav_bar">
<ul class="nav">
<li class="nav_li">Home</li>
<li class="nav_li">About</li>
<li class="nav_li selected">Blog</li>
<li class="nav_li">Book</li>
<li class="nav_li">Media</li>
<li class="nav_li">Events</li>
<li class="nav_li">Services</li>
<li class="nav_li">Contact</li>
<li class="search">
<input type="text" onfocus="if(this.value == 'Search') { this.value = ''; }" value="Search" />
</li>
<li class="search_btn">
<div class="search_button">Go</div>
</li>
</ul>
</div><!-- nav_bar -->
Now I've build pages before using this simple PHP code: <?php include("menu.php"); ?>
However, how would you guys 1st: organize this menu to accept and incoming value, like menu.php+select="home" and 2nd how would you pass in that value to add a class of selected to one of the Menu items?
First off your class on li "nav_li" is redundant and could be removed. Use .nav li in place to ref these list items with less lines of code. This works both as a JQuery selector and CSS selector. Second, to answer you actual question; I would use the active class as follows:
// Assuming the following html. <ul class="nav"><li>About</li></ul>
$('.nav li').click(function() {
$('.nav li.active').removeClass('.active');
$(this).addClass('.active');
window.location = $(this).children('a').attr('href');
return;
});
Now in the area where you keep your navbar you check to see if the current url is active by the following:
<?php
$currentUri = $_SERVER['REQUEST_URI'];
<ul class="nav">
<li class="<?php if($currentUri == '/about.php') {echo 'active';} ?>">About</li>
</ul>
Add the condition in the php script to each list item.
Sincerely,
Kevin
I assume you want your 'incoming value' for the purpose of highlighting a menu item (rather than displaying a particular page)? It's unnecessary, because the browser already knows what page it's on.
This is one way of highlighting the current page in jQuery:
$('.nav li a').each(function(){
var link = $(this).attr('href'),
current_page = location.href;
if(current_page.indexOf(link) != -1) {
$(this).addClass('current-page');
}
});
Before the include, set a value in some parameter, like $page = 'blog' and then in the menu.php
<li class="nav_li<?php echo $page === 'blog' ? " selected='selected'" : "" ?>">Blog</li>
This can be done using javascript
var url=document.location.toString();
if(url=="http://www.something.com/some.php")
{
//code to add class to specific link
}
else if(url=="http://www.something.com/someelse.php")
{
//code to add class to specific link
}
You can also do this on server side in menu.php
$url=$_SERVER['REQUEST_URI']
Then check the url and add class name accordingly
If I'm understainding what you're after, maybe something like:
index.php:
<?php
$page = 'home';
// page code ...
?>
about.php:
<?php
$page = 'about';
// page code ...
?>
etc, etc...
Then menu.php tweaked slightly:
<div id="nav_bar">
<ul class="nav">
<li class="nav_li home">Home</li>
<li class="nav_li about">About</li>
<li class="nav_li blog">Blog</li>
<li class="nav_li book">Book</li>
<li class="nav_li media">Media</li>
<li class="nav_li events">Events</li>
<li class="nav_li services">Services</li>
<li class="nav_li contact">Contact</li>
<li class="search">
<input type="text" onfocus="if(this.value == 'Search') { this.value = ''; }" value="Search" />
</li>
<li class="search_btn">
<div class="search_button">Go</div>
</li>
</ul>
</div><!-- nav_bar -->
and finally some javascript:
<script>
$(function () {
$('#nav_bar li.<?=$page?>').addClass('selected');
});
</script>
That assumes jQuery is being used, but it isn't necessary obviously, the same could be done with straight javascript.
I am using php includes to limit redundancy on my pages, how can I have my navigation have the current page selected with say a certain color for the HOME button when my navigation is called from a header.php file.
If i were to say to add a "active" class to the home.php item and add a style so it looked different, that would happen across the board for all my pages, which is why I am using includes in the first place, how can I have one header.php file with my navigation, that also allows each page the ability to show the current page you are on reflected in the navigation?
This is the nav portion of the header.php file
<ul>
<li>About Us</li>
<li>Portfolio</li>
<li>News</li>
<li>Contact</li>
</ul>
This is the index.php file that the header.php file is included in
<?php
include("includes/header.php");
?>
<div class="span-8" id="welcome">
<p>Lorem ipsum</p>
</div>
<div class="span-16 last" id="slideshow">
<img src="images/introImage1.png" alt="fountain">
<img src="images/introImage2.png" alt="bench">
<img src="images/introImage3.png" alt="bridge">
</div>
<?php
include("includes/footer.php");
?>
Try this:
<ul>
<?php
$pages = array('index.php' => 'About Us', 'portfolio.php' => 'Portfolio', 'news.php' => 'News', 'contact.php' => 'Contact');
foreach($pages as $url => $title) {
$li = '<li ';
if($_SERVER[ 'PHP_SELF' ] == $url) {
$li .= 'class="active"';
}
$li .= '>' . $title . '</li>';
echo $li;
}
?>
</ul>
Another option would be to set an attribute against the body tag, and then use a matched pair to change the styling of any number of elements.
So, on your main (home) page, you may have:
<body id="home">
Home
About Us
Then, within your CSS have:
body#home a.home , body#about a.about {
color:#999;background-color:#000; }
And, yet another option would be to include a single CSS statement inside the actual page.
<style type="text/css">
a[href="<?php echo basename( $_SERVER['PHP_SELF'] ); ?>"] ,
a[href="<?php echo $_SERVER['PHP_SELF']; ?>"] {
/* Styles for Current Page Links */ }
</style>
Of course, that is dependent on the browser being able to use that CSS Selector.
And, if you want to be completely sure that all links to the file are covered (including full URIs), then also include the following selector:
a[href="http<?php
echo ( $_SERVER['HTTPS'] ? 's' : '' ).'://'.
$_SERVER['HTTP_HOST'].(
( $_SERVER['HTTPS'] && $_SERVER['SERVER_PORT']!=443 )
|| ( !$_SERVER['HTTPS'] && $_SERVER['SERVER_PORT']!=80 ) ?
':'.$_SERVER['SERVER_PORT'] : '' ).
$_SERVER['PHP_SELF']; ?>"]
UPDATED: Corrected PHP Variables to use.