Just a few days ago I got a great script which basically allows the menu items at my static site to shrink and expand depending on how many list items there was in the menu. Now I’ve started work on converting the site to a WordPress installation and I’m having problems getting it to work because I’m not familiar with Javascript.
It won’t run the script, just the basic WordPress PHP.
This is the script:
$(document).ready(function() {
li_count = $('#main-navigation li').length;
div_size = $('#main-navigation').width();
new_li_font_size = (div_size/10)/li_count+'px';
new_li_width = 100/li_count+'%';
$('#main-navigation li').css('font-size', new_li_font_size);
$('#main-navigation li').css('width', new_li_width);
console.debug(new_li_size);
});
This is my previous nav:
<nav id="main-navigation">
<ul>
<li><a href="index.html">Hjem</a></li>
<li><a href="butikker-single.html">Butikker</a></li>
<li><a href="kampanjer.html">Kampanjer</a></li>
<li><a href="#">Ã
pningstider</a></li>
<li><a href="#">Om torvgårdens ting og tang</a></li>
</ul>
</nav><!-- END #main-navigation -->
This is my current nav:
<nav id="main-navigation" role="navigation">
<ul>
<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
</ul>
</nav><!-- END #main-navigation -->
I’ve been looking at the script and I think it could have something to do with the fact that there is no more li-tags in the php, but I might be wrong, considering I have no experience with jQuery. Is anyone able to help me out? 🙂
Just in case, here’s the implementation of jQuery which I put in my head:
<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
<script type="text/javascript" src="<?php bloginfo("template_url"); ?>/js/menu.js"></script>
Michael
To add a script in
WordPress
you can usewp_enqueue_script
in yourfunctions.php
fileAbove code will add the
script
in the head section of your page and you don’t need to use<script>
to add yourmenu.js
tag manually and this is the recommended way.Also you should use
Remove the script tag after the
wp_head()
you’ve used to load yourmenu.js
file and also change the following codeto (
wp_nav_menu()
generates anul
filled withli
tags)Also remember
nav
is anhtml5
tag, not supported in older browsers, you can use<div id="main-navigation"></div>
instead.