Trouble with a sub menu in my WordPress menu - php

I'm creating my first WordPress theme. It's a very simple one for a friend's website, who is a music producer. I'm creating the navigation menu. One of the items on the menu is listen. It's not an actual link. You hover over it and a submenu should appear. So I made Listen a custom link.
The problem is that the submenu doesn't appear. The Submenu has its own class. But I don't think WordPress is reading the CSS
Here is the CSS:
ul.sub-menu {
display: none;
position: absolute;
padding: 0px;
margin-left: -20px;
}
ul.sub-menu li {
display: block;
text-align: left;
background-color: grey;
}
ul.sub-menu li:hover {
background-color: #dae0e6;
}
ul.main_nav li:hover ul.sub-menu {
display: block;
}
Here is the HTML and php code in the header.php:
<nav>
<?php
$defaults = array(
'container' => false,
'theme_location' => 'primary-menu',
'menu_class' => 'main_nav'
);
wp_nav_menu( $defaults );
?>
<!--
<ul class="main_nav">
<li>
Home
</li>
<li>
Bio
</li>
<li>
<a>Listen</a>
<ul class="sub-menu">
<li>Originals</li>
<li>TV/Film</li>
</ul>
</li>
<li>Credits</li>
<li>Contact</li>
</ul>
-->
</nav>

Here is my approach:
I changed css so its shows menu sub menu with CSS.
JsFiddle Demo
nav
{
margin-top:15px
}
nav ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
nav ul a
{
display:block;
color:#333;
text-decoration:none;
font-weight:700;
font-size:12px;
line-height:32px;
padding:0 15px;
font-family:"HelveticaNeue","Helvetica Neue",Helvetica,Arial,sans-serif
}
nav ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
nav ul li:hover
{
background:#f6f6f6
}
nav ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff;
padding:0
}
nav ul ul li
{
float:none;
width:200px
}
nav ul ul a
{
line-height:120%;
padding:10px 15px
}
nav ul ul ul
{
top:0;
left:100%
}
nav ul li:hover > ul
{
display:block
}
<nav>
<ul class="main_nav">
<li>
Home
</li>
<li>
Bio
</li>
<li>
<a>Listen</a>
<ul class="sub-menu">
<li>Originals</li>
<li>TV/Film</li>
</ul>
</li>
<li>Credits</li>
<li>Contact</li>
</ul>
</nav>

In example above your menu is commented out. Also parent of ul.sub-menu should be positioned relative.

since you are new ... I think you should start by get general idea about the sub menu and style them in WordPress .
this Tut will help you
http://wproots.com/how-to-style-wordpress-menus-dropdowns/
also I will suggest other option for you .
start form other theme like wordpress deafult theme and edit it
start form starter theme like http://bootstrapwp.rachelbaker.me/
start from template
its hard to build theme from scratch if you are new ... step by step

please assign width to ul like
ul.sub-menu {
display: none;
}
and plaece
ul.main_nav li:hover ul.sub-menu {
display: block;
position: absolute;
padding: 0px;
margin-left: -20px;
top:0;
left:0;
width:800px;
}
just after it

Related

Adding Drop-down Menus to the Navigation Bar

I am just a beginner to HTML, CSS, PHP, javaScript.
My drop down menus to the navigation bar is not working.
When I keep the mouse over the links in navigation bar, no drop down menu is apeearing.
Need a help from some one.
CODE
<head>
#menu {
width:1060px;
margin:auto;
padding:0px 0px 0px 0px;
position:relative;
}
#menu ul {
height:25px;
font-size:20px;
font-family:Arial, Helvetica, sans-serif;
font-weight:bold;
text-align:center;
background-color:#8AD9FF;
border-radius:18px;
margin-top:10px;
margin-bottom:5px;
list-style:none;
}
#menu ul li{
border-style:solid;
border-width:2px;
border-bottom:none;
border-top:none;
border-color:#0000FF;
display:inline-block;
text-decoration:none;
color:#FF0000;
padding-bottom:0px;
padding-top:0px;
margin-right:-8px;
}
#menu ul li a {
padding-left:25px;
padding-right:25px
}
#menu ul li a:link{
color:#3366CC;
text-decoration:none;
}
#menu ul li a:visited{
color:#3366CC;
text-decoration:none;
}
#menu ul li a:hover{
background-color:#3399FF;
text-decoration:underline;
}
#menu ul ul {
display:none;
width:125px;
}
#menu ul li a:hover > ul {
margin:0px;
padding:0px;
}
#menu ul li a:hover > ul li {
float:none;
display:block;
}
#menu ul li a:hover > ul li a {
color:#3366CC;
min-width:150px;
}
#menu ul li a:hover > ul li a:hover {
background-color:#3399FF;
text-decoration:underline;
}
#menu ul li {position:relative;}
#menu ul li ul {
position:absolute;
top:30px;
left:0px;
}
</head>
<body>
<div id="menu">
<ul>
<li>Home</li>
<li>College
<ul>
<li>Faculties & Departments</li>
<li>Centres</li>
<li>College Facts</li>
<li>Awards</li>
</ul>
</li>
<li>Education
<ul>
<li>Pahrmacy Law Exam Review Course</li>
<li>Doctor Of Pharm Degree (Pharm. D.)</li>
<li>Graduate Programs</li>
<li>Online MS Programs</li>
</ul>
</li>
<li>Research
<ul>
<li>Medicina lChemistry</li>
<li>Doctor Of Pharm Degree (Pharm. D.)</li>
<li>Graduate Programs</li>
<li>Online MS Programs</li>
</ul>
</li>
<li>Faculty & Staff
<ul>
<li>Faculty Directory</li>
<li>Staff Dierctory</li>
</ul>
</li>
<li>News</li>
</ul>
</div>
</body>
Try something like this :
#menu ul li:hover > ul{
display: block;
}
Try to comment those line here :
#menu ul li ul {
position:absolute;
/*top:30px;*/
left:0px;
}
and :
#menu ul {
height:25px;
font-size:20px;
font-family:Arial, Helvetica, sans-serif;
font-weight:bold;
text-align:center;
background-color:#8AD9FF;
border-radius:18px;
/*margin-top:10px;*/
margin-bottom:5px;
list-style:none;
}
Look at the following code
#menu ul li a:hover > ul {
float:none;
display:block;
}
The above code will display the child levels when they hover the anchor element. But in your code it is a sibling element not child element. So use next sibling element syntax like below.
#menu ul li a:hover + ul {
float:none;
display:block;
}
But after this you have to do so many things to works your menu better way. This will fix the hover display issue only.
DEMO
You can do this without JS or PHP. You have an extra a tag in most of your CSS. You shouldn't show the nested ul when someone hovers over the anchor, you should show it when someone hovers over the list item:
#menu ul li ul {
display: none;
}
#menu ul li:hover ul {
display: block;
}
Likewise, the above style hides the entire nested ul instead of each li, which is cumbersome. Show and hide only one thing: the containing element.
I would recommend some more organization to keep things straight.
instead of writing ul ul{}, it may help you to write ul li ul{}, at least until this code becomes natural to you.
You also have a lot of extra pseudo elements in there. For instance:
#menu ul li a:hover > ul li a:hover {
background-color:#3399FF;
text-decoration:underline;
}
doesn't need the pseudo element :hover on the first a tag (which would be on the li tag anyway). Since you can only see the second a tag on hover, it can have this style all of the time:
#menu ul li ul li a:hover {
background-color:#3399FF;
text-decoration:underline;
}

PHP Include not working - php too complex? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm just branching my way out into php as I want to have a central menu file that I can edit instead of having to change the code on 20+ pages each time I update it! However my php include does not seem to be working. I'm wondering if maybe my php file I'm trying to include is too complex or doesn't work how I'm thinking (my understanding is that the php include basically takes all the text within it (in this case the html) and replaces the php include line with it)? There is also some css that would format the html when it was actually in the webpage, and I'm wondering if that is what is breaking it? Code is below.
html:
<div id="buttonstrip">
<?php include 'menubar.php';?>
</div>
php:
<?php
echo '<nav>
<ul>
<li>Computing
<ul>
<li>For Individuals
<ul>
<li>Repair</li>
<li>Maintenance</li>
<li>New Builds</li>
<li>Tuition</li>
</ul>
</li>
<li>For Business
<ul>
<li>Appraisals</li>
<li>Installation</li>
<li>Maintenance</li>
<li>Upgrades</li>
</ul>
</li>
</ul>
</li>
<li>Branding
<ul>
<li>Logos & Branding</li>
<li>Stationary</li>
<li>Guidelines</li>
</ul>
</li>
<li>Web Design
<ul>
<li>For Individuals</li>
<li>For Business</li>
</ul>
</li>
<li>Graphic Design
<ul>
<li>Illustration</li>
<li>Printwork</li>
<li>Merchandise</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>';
?>
CSS
nav ul {
/* [disabled]box-shadow: 0px 0px 9px rgba(0,0,0,0.15); */
padding: 10px 30px;
/* [disabled]border-radius: 10px; */
list-style: none;
position: relative;
z-index: 2;
display: block;
background-color: #1c6130;
background-position: top;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background-color: #FFFFFF;
}
nav ul li:hover a {
color: #1c6130;
}
nav ul li a {
display: inline;
padding: 20px 40px;
color: #ffffff;
text-decoration: none;
}
nav ul ul {
background: #ffffff; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
/* border-top: 1px solid #6b727c; */
/* border-bottom: 1px solid #575f6a; */
position: relative;
}
nav ul ul li a {
padding: 0px 30px;
color: #fff;
}
nav ul ul li a:hover {
background-color: #CCCCCC;
}
nav ul ul ul {
position: absolute; left: 100%; top:0;
}
Apologies if this is a completely novice question.
My guess: Your "html" file is actually named something.html, which causes the webserver to not recognize it as PHP. Rename it into something.php.
To verify that this was the problem, check the source of your HTML page, you should see the literal PHP code displayed there.
Might be you misnamed any file. just follow the instruction and keep all files in the same folder
File-index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="menu.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>My index page</title>
</head>
<body>
<div id="buttonstrip">
<?php include 'menubar.php';?>
</div>
</body>
</html>
Menu file-menu.php
<?php
echo '<nav>
<ul>
<li>Computing
<ul>
<li>For Individuals
<ul>
<li>Repair</li>
<li>Maintenance</li>
<li>New Builds</li>
<li>Tuition</li>
</ul>
</li>
<li>For Business
<ul>
<li>Appraisals</li>
<li>Installation</li>
<li>Maintenance</li>
<li>Upgrades</li>
</ul>
</li>
</ul>
</li>
<li>Branding
<ul>
<li>Logos & Branding</li>
<li>Stationary</li>
<li>Guidelines</li>
</ul>
</li>
<li>Web Design
<ul>
<li>For Individuals</li>
<li>For Business</li>
</ul>
</li>
<li>Graphic Design
<ul>
<li>Illustration</li>
<li>Printwork</li>
<li>Merchandise</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>';
?>
Menu css->menu.css
nav ul {
/* [disabled]box-shadow: 0px 0px 9px rgba(0,0,0,0.15); */
padding: 10px 30px;
/* [disabled]border-radius: 10px; */
list-style: none;
position: relative;
z-index: 2;
display: block;
background-color: #1c6130;
background-position: top;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background-color: #FFFFFF;
}
nav ul li:hover a {
color: #1c6130;
}
nav ul li a {
display: inline;
padding: 20px 40px;
}
<div id="buttonstrip">
<?php include ('menubar.php');?>
</div>
This appears correct. Only thing to check is the path of your include file.
Make sure your menubar.php is indeed located on the same folder as your script that includes it.
Make sure there are no spelling errors also on file name.
Create a menu.php file and include this file in php page where you want to show menu.
To include menu file in php file use <?php include "menu.php"; ?>
Below code is for menu.php
<div class="menu">
<ul>
<li>Insert</li>
<li>Update</li>
<li>Delete</li>
<li>Display</li>
<li>GUI</li>
</ul>
</div>

How can create menu for unlimited items?

I've a css menu, but this getting problem when I add more menu items.
What should I do to create a menu for unlimited menu items ?
my css
<style type="text/css">
#nav, #nav ul{
margin:0;
padding:0;
list-style-type:none;
list-style-position:outside;
position:relative;
line-height:1.5em;
}
#nav a:link, #nav a:active, #nav a:visited{
display:block;
padding:0px 5px;
border:1px solid #333;
color:#fff;
text-decoration:none;
background-color:#333;
}
#nav a:hover{
background-color:#fff;
color:#333;
}
#nav li{
float:left;
position:relative;
}
#nav ul {
position:absolute;
width:12em;
top:1.5em;
display:none;
}
#nav li ul a{
width:12em;
float:left;
}
#nav ul ul{
top:auto;
}
#nav li ul ul {
left:12em;
margin:0px 0 0 10px;
}
#nav li:hover ul ul, #nav li:hover ul ul ul, #nav li:hover ul ul ul ul{
display:none;
}
#nav li:hover ul, #nav li li:hover ul, #nav li li li:hover ul, #nav li li li li:hover ul{
display:block;
}
</style>
my sample menu for static items
<ul id="nav">
<li>1 HTML</li>
<li>2 CSS
<ul>
<li>3.1 jQuery
<ul>
<li>3.1.1 Download
<ul>
<li>3.1 jQuery
<ul>
<li>3.1.1 Download
<!-- If I add this ul here then it doesn't follow the current css.
<ul>
<li>3.1 jQuery
<ul>
<li>3.1.1 Download</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.2 Mootools</li>
<li>3.3 Prototype</li>
</ul>
-->
</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.2 Mootools</li>
<li>3.3 Prototype</li>
</ul>
</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.2 Mootools</li>
<li>3.3 Prototype</li>
</ul>
</li>
<li>3 Javascript
<ul>
<li>3.1 jQuery
<ul>
<li>3.1.1 Download</li>
<li>3.1.2 Tutorial</li>
</ul>
</li>
<li>3.2 Mootools</li>
<li>3.3 Prototype</li>
</ul>
</li>
</ul>
(suppose,I want to show product menu where product categories will come from database. so its also required to use some php,mysql.just show me how should I design the css menu for the unknown number of category and subcategory)
-Thanks.

Wordpress Custom Links in menu not working

I am using a blank theme template to create a wordpress theme and I enabled menus by adding the below code to header.php:
<body <?php body_class(); ?>>
<div id="navbar"></div>
<div id="wrapper" class="clear">
<nav>
<?php wp_nav_menu( array('theme_location' => 'primary' ) ); ?>
</nav>
And I added this to functions.php:
if (function_exists('register_nav_menus')) {
register_nav_menus(
array(
'main_nav' => 'Main Navigation Menu'
)
);
};
The menu shows up and works fine when I add links to pages but when I add a custom link to say http://www.google.com with the label Google nothing shows up in the navigation bar. When I view the source the custom link is not being populated at all.
<nav>
<div class="menu">
<ul>
<li class="page_item page-item-9">Anatomy</li>
<li class="page_item page-item-11">History</li>
<li class="page_item page-item-7">Home</li>
</ul>
</div>
</nav>
My css for the nav is simple:
nav {
padding:0;
margin:0;
position:absolute;
width: 900px;
}
nav ul li
{list-style: none;
font-family: 'Numans', sans-serif;
font-size: 15px;
color: #ffffff;
text-align:left;
text-transform: uppercase;
padding: 0;
margin: 0 30px;
display: inline;
position: relative;
top:-32px;
}
nav ul li a {
text-decoration: none;
color: #ffffff;
}
nav ul li a:visited
{color: #ffffff;
}
nav ul li a:hover
{color: #cccccc;
}
nav ul li a:active
{color: #ffffff;
}
I am very new to php and wordpress theming and I can't find anyone online with the same issue.
The issue is that your theme_location does not match the name you assigned when you registered the menu.
When you registered the menu, you called it main_nav. Since you called it that, in order to display that menu, you need to change the call to the menu to reference main_nav, like so:
wp_nav_menu( array('theme_location' => 'main_nav' ) );

How to make a link stay active in CSS?

Basically I want the hover-look of a link stay active when you're on that site. I hope I'm explaining this right. There's supposed to be a background behind the link when you're visiting that specific page.
Here's the code for the html:
<div class="menudiv">
<div id="menu">
<ul id="menu">
<li><span>Hem</span></li>
<li><span>Om oss</span></li>
<li><span>Tjänster</span></li>
<li><span>Referenser</span></li>
<li><span> Kontakt</span></li>
</ul>
<div class="clr"></div>
</div>
</div>
And here's the css:
#menu { float:right; padding:23px 0 0 0; margin:0; width:420px; height:35px;}
#menu ul { text-align:right; padding:0; margin:0; list-style:none; border:0; height:35px;}
#menu ul li { float:left; margin:0; padding:0 5px; border:0; height:35px;}
#menu ul li a { float:left; margin:0; padding:10px 0; color:#5c8783; font:normal 12px Arial, Helvetica, sans-serif; text-decoration:none;}
#menu ul li a span { padding:10px 13px; background:none;}
#menu ul li a:hover { background:url(images/r_menu.gif) right no-repeat;}
#menu ul li a:hover span { background:url(images/l_menu.gif) left no-repeat;}
#menu ul li a:active { background:url(images/r_menu.gif) right no-repeat;}
#menu ul li a:active span{ background:url(images/l_menu.gif) left no-repeat;}
Add a CSS class for the that link (eg, <a class="CurrentPage">), then apply the selector to that class (eg, #menu ul li a:active, a.CurrentPage)
As much I know the hover property should put last in CSS (below the active and others) in order to work properly.
you are giving same id :
<div id="menu">
<ul id="menu">
which is wrong
you should make ids different.
What you basically need is to add a css class to the a tag in your menu.
example:
<li><span>Hem</span></li>
add styling to your currenPage class as per the designs you want for that menu item
now, your page should be dynamic for you to be able to set the currenPage css class to the current menu item where the user is currently viewing
example:
<li><a href="?p=start" class="<?php if(/**check if the user is in this link/menu item**/) echo "currentPage"; ?>" ><span>Hem</span></a></li>

Categories