How to show different header menu in the different page using php? - php

I have two PHP scripts:
header.php
contact_us.php
In Index.php when the user clicks the menu, it smoothly slides down.
But for the "Contact Us" page I don't want that behavior; because it is so simple, containing only a link, I just want it to come up quickly and remain until the home link is clicked in the menu.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<?php
if(basename($_SERVER['SCRIPT_FILENAME']) == 'index.php'){
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}else{
?>
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>
<?php
}
?>
</div>

For else condition when you are on contact-us page.
Change URL as below:
<nav>
<ul class="nav navbar-nav">
<li>Home </li>
<li>Services </li>
<li>Search For Homes </li>
<li>Recent Transacctions</li>
</ul>
</nav>

Related

how to convert static HTML5 navigation menu to wordpress dynamic menu

How to convert static HTML5 navigation menu to WordPress dynamic menu.
I want to output this HTML menu in WordPress.and I don't know how to make ul and li in this form.
Thanks for your help.
This is my HTML5 navigation menu
<ul class="nav topnav">
<li class="dropdown active">
<i class="icon-home"></i> Home <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Homepage 2</li>
<li>Homepage 3</li>
<li>Parallax slider</li>
<li>Landing page</li>
</ul>
</li>
<li class="dropdown">
Features <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Typography</li>
<li>Components</li>
<li>Icons</li>
<li>Icon variations</li>
<li class="dropdown">3rd menus<i class="icon-angle-right"></i>
<ul class="dropdown-menu sub-menu-level1">
<li>Sub menu</li>
<li>Sub menu</li>
</ul>
</li>
</ul>
</li>
<li class="dropdown">
Pages <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>About us</li>
<li>FAQ</li>
<li>Team</li>
<li>Services</li>
<li>Pricing boxes</li>
<li>404</li>
</ul>
</li>
<li class="dropdown">
Portfolio <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Portfolio 2 columns</li>
<li>Portfolio 3 columns</li>
<li>Portfolio 4 columns</li>
<li>Portfolio detail</li>
</ul>
</li>
<li class="dropdown">
Blog <i class="icon-angle-down"></i>
<ul class="dropdown-menu">
<li>Blog left sidebar</li>
<li>Blog right sidebar</li>
<li>Blog fullwidth</li>
<li>Post left sidebar</li>
<li>Post right sidebar</li>
</ul>
</li>
<li>
Contact
</li>
</ul>
</nav>
I want to convert it to WordPress menu with
wp_nav_menu()
How can I make this custom menu?
I don't want to use any plugins to make custom menu.
First, you need to register nav menu in functions.php like this:
function register_menu(){
register_nav_menu('main-menu', 'Main menu');
}
add_action('init', 'register_menu');
When you do this, new option will appear in your admin panel. Go to Appearance > Menus and start adding links, pages or whatever you want to be in your menu. You can specify depth as well. Once you are done with your menu, select location of the menu, which will in this case be your newly created 'Main menu'. Now you want to go back to your page template in code editor to display that menu. It is very hard to use wp_nav_menu() function to display such complex menu so we will use different approach. We will grab menu items for start:
$menuLocation = get_nav_menu_locations();
$mainMenuID = $menuLocation['main-menu'];
$topMenuItems = wp_get_nav_menu_items($mainMenuID);
Now goes the main part. We are looping through menu items and displaying it's data in combination with custom html elements:
<?php
$menuLocation = get_nav_menu_locations();
$mainMenuID = $menuLocation['main-menu'];
$topMenuItems = wp_get_nav_menu_items($mainMenuID);
// check if menu has items
if(!empty($topMenuItems)){
?>
<ul>
<?php
foreach ($topMenuItems as $topMenuItem){
//Check if menu_item_parent property is set to 0.
That means that menu item is top level item.
if($topMenuItem->menu_item_parent == 0){
$topItemID = $topMenuItem->ID;
$submenuItems = array();
//check if there are submenu items for current top menu
item.
foreach ($topMenuItems as $submenuItem){
if($submenuItem->menu_item_parent == $topItemID){
$submenuItems[] = $submenuItem;
}
}
?>
<li>
<?php echo $topMenuItem->title ?>
<?php
// If there are submenu items for current item, display
them.
if(!empty($submenuItems)){
?>
<ul class="submenu">
<?php
foreach ($submenuItems as $subitem){
?>
<li>
<?php echo $subitem->title ?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
</li>
<?php
}
}
?>
</ul>
<?php
}
?>
// Using this logic, you can go in depth how much you want to.

Make a global sidebar?

I'm new at web programming was was wondering if you guys could help me. What I want I guess must be easy, but can't really find /how/.
I have my cute little sidebar that isn't even finished and is constantly needing to be changed. I already gave up on updating it until everything is ready:
<!-- sidebar menu -->
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Search</li>
<li>Dashboard</li>
<li>Lookup</li>
</ul>
</li>
<li><a><i class="fa fa-cubes"></i> Inventory <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Asset List
<li><a>Asset Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="create.php">Create Assets
</li>
<li>Update Assets
</li>
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-sitemap"></i> Tickets <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Ticket Overview
<li><a>Ticket Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">Create a Ticket
</li>
<li>Update a Ticket
</li>
</ul>
</li>
<li><a>User Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="sub_menu">User List
</li>
<li>Create a User
</li>
<li>Update a User
</li>
</ul>
</li>
</ul>
<li><a><i class="fa fa-laptop"></i> Loans <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li><a>Loan Management<span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li class="create.php">Create a Loan
</li>
<li>View Loans
</li>
</ul>
</li>
</ul>
</li>
</li>
</ul>
</div>
</div>
<!-- /sidebar menu -->
But the problem is that I'm doing it like an uncultured swine and keeping that in every single page where it is needed. And if I want to make a change, I need to edit every single page.
I was wondering if there was a way to have a class, or something, with my header, sidebar, and footer, and load them across all pages that needed them?
Thank you for your help!
You can extract the sidebar code into its own PHP file (sidebar.php) then include it where you need it with include or include_once
<html>
...
<body>
<?php include './sidebar.php' ?>
...
</body>
</html>
Using include is essentially the same as copy pasting the code into your file. I.e. It will be added where you include the code and it will be evaluated there as well
Its very simple, Save you sidebar in a php file lets call it sidebar.php, and add below code in your other PHP file where you want to show the sidebar.
<?php include_once('sidebar.php');?>
That's it.

Wordpress Plugin Opening-Hours on custom theme

I am learning to build a custom Wordpress Theme from scratch. I would like to display opening times in the footer. I am trying to use this plugin but i can not find any documentation on it! I just want users to add their opening and close times using this and then output the opening/close times in the footer html. Does anyone have any experience using this plugin / know a better way i can achieve this?
HTML for foot.php
<div class="w-col w-col-6 w-col-stack">
<h3 class="heading-6">OPENING TIMES</h3>
<div class="open-times-wrapper">
<ul class="unordered-list">
<li class="open-li">MONDAY</li>
<li class="closeinfo open-li">USER INPUT GOES HERE!</li>
</ul>
<ul class="unordered-list">
<li class="open-li">TUESDAY</li>
<li class="closeinfo open-li">10:30am - 00:00pm</li>
</ul>
<ul class="unordered-list">
<li class="open-li">WEDNESDAY</li>
<li class="closeinfo open-li">10:30am - 00:00pm</li>
</ul>
<ul class="unordered-list">
<li class="open-li">THURSDAY</li>
<li class="closeinfo open-li">10:30am - 00:00pm</li>
</ul>
<ul class="unordered-list">
<li class="open-li">FRIDAY</li>
<li class="closeinfo open-li">10:30am - 00:00pm</li>
</ul>
<ul class="unordered-list">
<li class="open-li">SATURDAY</li>
<li class="closeinfo open-li">10:30am - 00:00pm</li>
</ul>
<ul class="unordered-list">
<li class="open-li">SUNDAY</li>
<li class="closeinfo open-li">10:30am - 00:00pm</li>
</ul>
</div>
</div>
Read the plugin description. All the documentation is on their Github: https://github.com/janizde/WP-Opening-Hours

Keep navigation bar an all pages

hope you fine and well,
i have a navigation bar like this :
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
how i can show this bar in all my html and php pages !
regards.
Put the render code in a my_navbar.php
echo '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>';
and then in all php file you need the navbar put
<?php
include_once "my_navbar.php"; // this will include a.php
?>
Use include_once for avoiding multiple include
To expand on scaisEdge's comment, here's how you can do this in an include.
In nav.php (name it however you want):
<?php
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
?>
Then where you want the navigation:
<?php include('nav.php'); ?>
You can use include(), i.e.:
navigation.php
<?php
echo <<< EOF
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a onclick="load_home()"href="#">About</a>
</li>
<li>
Services
</li>
<li>
Contact
</li>
</ul>
</div>
EOF;
anotherfile.php
<?php
include("navigation.php");
//the rest of the code...
include vs include_once:
The include_once and require_once functions are slower than include
and require, simply because they keep track of the files that have
already been included, to avoid including them more than once.

I Want to Hide a Link in my Navbar While Logged In (Using CodeIgniter as my framework)

I'm attempting to hide the "Register" link in my navbar after the page checks if someone is logged in. I'm using CodeIgniter as my framework.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home <span class="sr-only">(current)</span></li>
<li>Reports <span class="sr-only">(current)</span></li>
<li >Register <span class="sr-only">(current)</span></li>
</ul>
I used this to check if someone is logged in before it shows the logout button and it works fine and dandy.
<?php if($this->session->userdata('logged_in')): ?>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
<?php endif;?>
However, I've seen to run into a snag when trying to do it to hide the Register link. Any help would be much appreciated!
Its should be like this
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home <span class="sr-only">(current)</span></li>
<li>Reports <span class="sr-only">(current)</span></li>
<?php if(!$this->session->userdata('logged_in')){ ?>
<li >Register <span class="sr-only">(current)</span></li>
<?php } ?>
</ul>

Categories