Add div's into wordpress navigation - php

I am building a custom wordpress theme using underscores and want to add two div's inside the navigation. I know I have to use a custom Walker, but I don't know exactly how and all i can find is how to customize the list items themselves. But all I need is to div's on top of the generated list.
This is what i get now:
<div class="menu-all-pages-container">
<ul id="primary-menu" class="menu">
<li>Menu item 1</li>
<li>Menu item 1</li>
...etc
</ul>
</div>
What I want to have:
<div class="menu-all-pages-container">
<div id="mydiv"></div>
<div class="button"></div>
<ul id="primary-menu" class="menu">
<li>Menu item 1</li>
<li>Menu item 1</li>
...etc
</ul>
</div>
As I said, I think I need a custom Walker and customize the start_el() function? But I am lost after that..
Thanks in advance :)

You can use wp_nav_menu_items filter. try this:
add_filter('wp_nav_menu','add_custom_nav_elements', 10, 1);
function add_custom_nav_elements( $nav ) {
$elements = '<div id="mydiv"></div><div class="button"></div>';
return $elements . $nav;
}
If you looking for add elemets only for primary menu, try this:
add_filter('wp_nav_menu','add_custom_nav_elements', 10, 2);
function add_custom_nav_elements( $nav, $args ) {
$elements = '<div id="mydiv"></div><div class="button"></div>';
if( $args->theme_location == 'primary' )
$nav = $elements . $nav;
return $nav;
}
And add this in where you want to add menu:
<div class="menu-all-pages-container">
<?php wp_nav_menu(array('theme_location' => 'primary')); ?>
</div>

Related

Add active class to WordPress nav

I have pretty long nav here, which I have to import to WordPress.
<header class="small">
<div class="yellow-stripe"></div>
<div class="container">
<nav role="navigation">
<ul class="navigation">
<li>
About us
<ul class="sub-navigation">
<li>We are</li>
<li>Our story</li>
<li>Why Ledil</li>
<li>Where</li>
<li>Management</li>
<li>Investors</li>
</ul>
</li>
<li>News</li>
<li>Events</li>
<li>FAQ</li>
</ul>
</nav>
<ul class="lang">
<li class="active">EN</li>
<li>ES</li>
<li>RUS</li>
</ul>
</div>
<div class="nav-button">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</header>
It's made in HTML and now I have to import it to WordPress. I got WordPress to include the header, and to load the whole thing just fine, and it looks great on page, but I would need to add class="active" for bootstrap nav to current page and to child pages too.
I found some answers by googling, but I didn't really get very far.
My function.php looks like this:
<?php
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
add_filter('nav_menu_css_class', 'special_nav_class', 10, 2);
function special_nav_class($classes, $item){
if( in_array('current_page_parent', $classes) ){
$classes[] = 'active ';
}
return $classes;
}
?>
I'm not really sure what I should try. I find the whole WordPress thing very strange.
There seems to be no current_page_item class here.. should I do anything special to enable it?
li.current_page_parent a {} won't work, nor will setting class="<?php if (is_page('name-of-page')) echo 'active'; ?>" inside a tags... goddamn WordPress...
Never mind.. I'm just bit slow.... I put class="<?php if (is_page('why')) echo 'active'; ?>" inside every li element.
If anyone ends up here:
You should check if the page is active or the page is child of another page
<li class="<?php if (is_page('offices')) echo 'active'; ?>">Offices</li>
Checks if page is "offices" and
<li class="<?php if (is_page_child(143)) echo 'active'; ?> echo 'active'; ?>">
Checks if page is child page of page with id "143" and then echoes "active" inside class=" "

Child pages not showing as active

I'm have an issue with a WordPress theme that I am making using Twitter Bootstrap.
I have two menu's, one of which is a Nav_Walker to which that displays correctly and highlights an active post category if selected, however, the next menu is used to display child pages on the parent page and is using code to do it (See Code). It outputs all the child pages correctly but it doesn't trigger active on the child page menu entry that is being looked at therefor I can't set the active menu item to look any differently.
<?php
if ($post->post_parent)
$children = wp_list_pages("title_li=&child_of=" . $post->post_parent . "&echo=0");
else
$children = wp_list_pages("title_li=&child_of=" . $post->ID . "&echo=0");
if ($children) {
?>
<?php } ?></pre>
<ul class="nav nav-tabs nav-stacked uppercase">
<li class="menu-heading"><h6>OVERVIEW</h6></li>
<?php echo $children; ?>
</ul>
The the code outputs:
<ul class = "nav nav-tabs nav-stacked uppercase">
<li class = "menu-heading"><h6>OVERVIEW</h6></li>
<li class = "">SUB PAGE 1</li>
<li class = "">SUB PAGE 2</li>
<li class = "">SUB PAGE 3</li>
</ul>
The class for the li remains empty when the page is active.
Does anyone know how I can work around this?
Thanks in advance!!

need better PHP script for an if else condition to select current menu item

Using the following PHP script to select the page Im currently on.
each page has the variable $page== 'pagename'
This script works fine but sometimes the buttons need to be rolled over twice in order for the dropdown menu to appear.
Is there a better way?
<div id="navigation">
<ul id="nav">
<!-- If the button HOME is selected then make the HOME Button Active -->
<?php if ($page == 'home') { ?><li>HOME</li>
<?php } else { ?><li>HOME<?php } ?>
<!-- If the button ABOUT US is selected then make the ABOUT US Button Active -->
<?php if ($page == 'about-us') { ?><li>ABOUT US
</li>
<?php } else { ?><li>ABOUT US
</li>
<?php } ?>
<!-- If the page projects is selected then make the Projects Button Active -->
<?php if ($page == 'projects') { ?><li>PROJECTS
<ul>
<li>Project 1</li>
<li>Project 2</li>
<li>Project 3</li>
<li>Project 4</li>
</ul>
</li>
<?php } else { ?><li>PROJECTS
<ul>
<li>Project 1</li>
<li>Project 2</li>
<li>Project 3</li>
<li>Project 4</li>
</ul>
</li>
<?php } ?>
<!-- If the page Capabilities is selected then make the Capabilities Button Active -->
<?php if ($page == 'capabilities') { ?><li>CAPABILITIES
<ul>
<li>Civil Works</li>
<li>Commercial Construction</li>
<li>Controlled Waste Management</li>
<li>Plant Hire</li>
</ul>
</li>
<?php } else { ?><li>CAPABILITIES
<ul>
<li>Civil Works</li>
<li>Commercial Construction</li>
<li>Controlled Waste Management</li>
<li>Plant Hire</li>
</ul>
</li>
<?php } ?>
<!-- If the page Careers is selected then make the Careers Button Active -->
<?php if ($page == 'careers') { ?><li>CAREERS</li>
<?php } else { ?><li>CAREERS
</li>
<?php } ?>
<!-- If the page Contact Us is selected then make the Contact Us Button Active -->
<?php if ($page == 'contactus') { ?><li>CONTACT US</li>
<?php } else { ?><li>CONTACT US
</li>
<?php } ?>
</ul>
<!-- Search Form -->
<div class="search-form">
<form method="get" action="#">
<input type="text" class="search-text-box" />
</form>
</div>
</div>
<div class="clear"></div>
</div>
<!-- Navigation / End -->
The second part could be smaller:
<?php if ($page == 'projects') { ?><li>PROJECTS <?php } else { ?> <li>PROJECTS <?php } ?>
<ul>
<li>Project 1</li>
<li>Project 2</li>
<li>Project 3</li>
<li>Project 4</li>
</ul>
</li>
So basically on which page your are on, that should have id="current"? This can be done the following way:
<?php
$selected = array($page => ' id="current"');
?>
...
<li><a href="index.php"<?php print $selected['home']; ?>></li>
<li><a href="about-us.php"<?php print $selected['about-us']; ?>>ABOUT US</a></li>
...
The array $selected will have one index which is the current page and the value will be id="current". So this value will be applied to the active page only.
However this will trigger an undefined index notice, but you won't need any ifs in your code.
If you are okay with a bit more complex structure you could do it like this and won't get notices.
<?php
$selected = array($page => ' id="current"');
?>
...
<li><a href="index.php"<?php print isset($selected['home'])?$selected['home']:''; ?>></li>
<li><a href="about-us.php"<?php print isset($selected['about-us'])?$selected['about-us']:''; ?>>ABOUT US</a></li>
...
Since you are basically doing the same thing over and over again, I'd suggest automating the whole thing anyway.
<?php
$naviPages = array(
array('name' => 'HOME', 'url' => 'index.php'),
array('name' => 'ABOUT US', 'url' => 'about-us.php'),
array('name' => 'PROJECTS' => 'url' => 'projects.php', 'children' => array(
array('name' => 'Project 1', 'url' => 'project-01.php'),
array('name' => 'Project 2', 'url' => 'project-02.php'),
)),
//...
);
//...
foreach ($naviPages as $naviPage) {
print '<li><a href="'.$naviPage['url'].'"';
if ($page == $naviPage['name'])
print ' id="active"';
print '>'.$naviPage['name'].'</a>';
if (isset($naviPage['children'])) {
print '<ul>';
foreach ($naviPage['children'] as $naviPageChild) {
print '<li>'.$naviPageChild['name'].'</li>';
}
print '</ul>';
}
print '</li>';
}
I would tend to stick the $page as a css class in your <body> tag or the navigation div. Then you can do just about everything else in pure css!
So for example, say you have this:
<div id="navigation" class="page-<?php echo $page ?>">
<ul id="nav">
<li class="nav-home">HOME
<li class="nav-about">ABOUT US</li>
Notice how the navigation div has the page name, and each menu item has a unique class name. Now all you have to do is use some CSS:
.page-home .nav-home,
.page-about .nav-about {
color: red; /** assuming you want your active/current menu item to be red **/
}
See how much cleaner that is?
Of course you can then extend this to show/hiding the sub-menus. Give your sub-menu <ul> tag a unique classname:
<li class="nav-projects">PROJECTS
<ul class="submenu sub-projects">
...
</ul>
And some CSS to control that:
.submenu {
display: none; /** Our default **/
}
.page-projects .sub-projects {
display: block; /** Show the projects sub-menu when you are on that page **/
}

Add a div and change the classes or navigation menu using wordpress walker?

I want to know how to make a WordPress wp_nav_menu() or wp_list_pages() Walker class that will generate nav menu output like this?
<!-- Level1 -->
<li class="current">
Name
<!--Level2-->
<div class="nav-sub">
<ul>
<li>Name</li>
<li>
Name
<!--Level3-->
<div class="nav-sub">
<ul>
<li>
Name
<!--Can Add Unlimited levels If Possible-->
</li>
<li>Name
</li>
</ul>
</div>
</li>
<li>Name</li>
<li>Name</li>
</ul>
</div>
</li>
<!-- This is One Level only -->
<li>
Name
</li>
I tried many ways but can't just figure it out.
Here is the current output using wp_list_pages()
<ul class="myclass">
<li id="home">Home</li>
<li class="page_item page-item-2 page_item_has_children">Sample Page
<ul class="children">
<li class="page_item page-item-9">Home</li>
</ul>
</li>
<li class="page_item page-item-19">page no sub</li>
</ul>
Thanks in advance!
P.S: I have already read this article here http://codex.wordpress.org/Function_Reference/wp_nav_menu But can't figure out :(
Edit :
Well, look the differents parameters and call your menu
<?php
class Child_Wrap extends Walker_Nav_Menu
{
function start_lvl(&$output, $depth)
{
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<div class=\"div-sub\"><ul class=\"sub-menu\">\n";
}
function end_lvl(&$output, $depth)
{
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul></div>\n";
}
}
wp_nav_menu(
array(
'menu' => 'navigation',//(i created this menu on the backend of wordpress)
'container' => 'div',
'container_class' => 'menu-sidebar',
'menu_class' => 'menu-class',
'menu_id' => 'menu-id',
'theme_location' => 'primary',
'walker' => new Child_Wrap()
)
); ?>
You can specified many parameters (id menu, class and id container etc...)
source class walker
Should you find your happiness here
Name menu screen ('menu' => 'navigation')

Adding a first and last class to Wordpress' widget contents

In Wordpress, I'm looking for some way to add a "last" and a "first" class to list items inside Wordpress widgets. The HTML could look like this:
<div class="widget-area">
<ul >
<li class="widget_recent_comments">
<h3 class="widget-title">Recent comments</h3>
<ul id="recentcomments">
<li class="recentcomments">Comment 1</li>
<li class="recentcomments">Comment 2</li>
<li class="recentcomments">Comment 3</li>
<li class="recentcomments">Comment 4</li>
</ul>
</li>
<li class="widget_my_links">
<h3 class="widget-title">My links</h3>
<ul id="my-links">
<li class="item">Link 1</li>
<li class="item">Link 2</li>
<li class="item">Link 3</li>
<li class="item">Link 4</li>
<li class="item">Link 5</li>
</ul>
</li>
</ul></div>
In this example above i'd like to have first/last classes added to the li with "Comment 1", "Comment 4", "Link 1" and "Link 5".
Is there an easy workaround for this? (I don't want to do this with javascript)
Thank you.
I'm guessing these lists are generated in a loop. So what you could do, is create a variable before you go into the loop, and set it's value to 1 ($i = 1). Than at the end of the loop, add one up ($i++). Now, where you want the first/last class to appear, you can do
<?php if($i == 1):
echo ' first';
elseif( $i == $number_of_items )
echo 'last';
endif;
?>
At $i == $number_of_items, you are comparing the max with the current, so you know you have the last if the statement is true.
Hope this answers your question.
Well the first-item is easy, just use
ul#my-list li:first-child {
/* special styles */
}
It's not adding a class, but you can still style it. There is not a similar css rule for :last-child unfortunately

Categories