Skip to main content

WordPress: Foundation 6 Top-Menu

Foundation Walker

class FoundationMenu extends Walker_Nav_Menu
{

  function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output )
  {
    $element->has_children = !empty( $children_elements[$element->ID] );
    $element->classes[] = ( $element->current || $element->current_item_ancestor ) ? 'active' : '';
    $element->classes[] = ( $element->has_children ) ? 'has-dropdown' : '';
    parent:: display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  }

  function start_el( &$output, $object, $depth = 0, $args = array ( ) , $current_object_id = 0 )
  {
    $item_html = '';
    parent:: start_el( $item_html, $object, $depth, $args );
    $output.= ( $depth == 0 ) ? '<li class = "divider"></li>' : '';
    $classes = empty( $object->classes ) ? array ( ) : ( array ) $object->classes;
    if( in_array( 'label', $classes ) ){
      $output.= '<li class = "divider"></li>';
      $item_html = preg_replace( '/<a[^>]*>( .* )<\/a>/iU', '<label>$1</label>', $item_html );
    }
    if ( in_array( 'divider', $classes ) ){
      $item_html = preg_replace( '/<a[^>]*>( .* )<\/a>/iU', '', $item_html );
    }
    $output.= $item_html;
  }

  function start_lvl( &$output, $depth = 0, $args = array ( ) )
  {
    $output.= "<ul class=\"menu vertical dropdown\" data-dropdown-menu>";
  }
}



Save this file as foundation-menu.php

functions.php

function foundation_menu_left()
{
  wp_nav_menu(array(
              'container' => false,
              'container_class' => '',
              'menu' => 'header-left',
              'theme_location' => 'header-left',
              'menu_class' => 'dropdown menu',
              'before' => '',
              'after' => '',
              'link_before' => '',
              'link_after' => '',
              'depth' => 0,
              'fallback_cb' => false,
              '$items_wrap' => '<ul class="menu vertical dropdown" >',
              'items_wrap' => '<ul id="%1$s" class="%2$s" data-dropdown-menu data-options="closingTime:50;alignment:right;">%3$s</ul>',
              'walker' => new FoundationMenu()
              )
  );
}

function foundation_menu_right()
{
  wp_nav_menu(array(
              'container' => false,
              'container_class' => '',
              'menu' => 'header-right',
              'theme_location' => 'header-right',
              'menu_class' => 'dropdown menu',
              'before' => '',
              'after' => '',
              'link_before' => '',
              'link_after' => '',
              'depth' => 0,
              'fallback_cb' => false,
              '$items_wrap' => '<ul class="menu vertical dropdown" >',
              'items_wrap' => '<ul id="%1$s" class="%2$s" data-dropdown-menu data-options="closingTime:50;alignment:right;">%3$s</ul>',
              'walker' => new FoundationMenu()
              )
  );
}
require_once get_template_directory().'/foundation-menu.php';

header.php

 <div class="top-bar">
    <div class="top-bar-title">
      <span data-responsive-toggle="responsive-menu" data-hide-for="medium">
        <span class="menu-icon" data-toggle></span>
      </span>
      <a href="<?= $url;?>"><strong>TITLE</strong></a>
    </div>
    <div id="responsive-menu">
     <div class="top-bar-left">
       <?php foundation_menu_left();?>
     </div>
     <div class="top-bar-right">
       <?php foundation_menu_right();?>
     </div>
   </div>
 </div>

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...