Code Monkey home page Code Monkey logo

wordpress-bem-menu's Introduction

Wordpress BEM Menu - Forked and adjusted for SUIT CSS syntax

Say goodbye to badly named menus in Wordpress and say hello to Wordpress BEM Menus!

To use simply drop the contents of wp_bem_menu.php into your functions.php file.

Usage

First you need to register a nav menu in your themes functions.php file.

register_nav_menu('my_menu', 'primary site menu');

You also need to create a menu in wp-admin and make sure it is assigned a theme location that matches your registered nav menu above.

Then insert the following function into your theme. The first argument is the theme location (as defined in wp-admin) and the second argument is the class prefix you would like to use for this particular menu. The class prefix will be applied to the menu <ul> and every child <li> as the 'block'. The third optional argument accepts either an array() or a string.

<?php bem_menu('menu_location', 'Nav', 'Nav--myModifier'); ?>

If you want to add multiple modifiers to the <ul> use an array e.g:

<?php bem_menu('menu_location', 'Nav', array('Nav--myModifier','Nav--myOtherModifier')) ?>

Please note that these modifier classes are not inherited by descendants. this is by design to avoid bloated navigation markup. You can still target children of a specific modifier like so: .Nav--myModifier .Nav-item{}

html output

<ul class="my-menu my-menu--my-modifier">
    <li class="Nav-item is-active"><a href="#">Home</a></li>
    <li class="Nav-item"><a href="#">Page 2</a></li>
    <li class="Nav-item"><a href="#">Page 3</a></li>
</ul>

Css classes

The syntax is very simple, all menu items are logically grouped by depth to avoid some of the nesting issues of the standard output.

/* Top level items */
.Nav-item{}

/* Parent item */
.Nav-item--parent{}

/* Active page item */
.Nav-item.is-active{}

/* sub menu class */
.Nav-Sublist{}

Modification

BEM syntax is very subjective and different developers use different conventions. If you wish to change or adapt the syntax to go with your own implemntation all the menu suffixes are contained within the $this->item_css_classes array:

$this->item_css_classes = array(
    'list'                      => $this->css_class_prefix . 'List',
    'item'                      => $this->css_class_prefix . 'Item',
    'link'                      => $this->css_class_prefix . 'Link',
    'parent_item'               => $this->css_class_prefix . 'Item--parent',
    'sub_menu'                  => $this->css_class_prefix . 'Sublist',
    'sub_menu_item'             => $this->css_class_prefix . 'Item',
    'active_item'               => 'is-active',
    'parent_of_active_item'     => '',
    'ancestor_of_active_item'   => ''
);

wordpress-bem-menu's People

Contributors

roryashfordbentley avatar vincentorback avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.