【问题标题】:Add (BEM) classes to Wordpress menu list-items and anchors将 (BEM) 类添加到 Wordpress 菜单列表项和锚点
【发布时间】:2017-04-05 04:00:05
【问题描述】:

我从构建静态 html 模板开始我的项目。我的“主要”导航如下所示:

<nav class="site-nav">
    <a href="#" class="page-head__logo">
        <img src="img/interface/logo.png" alt="Wireforce logo" />
    </a>
    <a href="#" id="site-nav__toggle">Menu</a>
    <ul class="site-nav__list">
        <li class="site-nav__item"><a href="#" class="site-nav__link">Services</a></li>
        <li class="site-nav__item"><a href="#" class="site-nav__link">Security</a></li>
        <li class="site-nav__item"><a href="#" class="site-nav__link">Blog</a></li>
        <li class="site-nav__item"><a href="#" class="site-nav__link">About</a></li>
        <li class="site-nav__item"><a href="#" class="site-nav__link">Contact</a></li>
    </ul>
</nav>

我知道我可以使用 .site-nav li a {} 在 css 中定位 Wordpress 锚点,但我想保持我的 BEM 命名约定以保持一致性。

functions.php 中,我可以看到使用wp_nav_menu 我可以为容器/ul 指定类,但不能为列表项/锚指定类。谁能推荐实现我所追求的“最佳”方式?做一些阅读,似乎 walker 类可能是要走的路?所以我真的很感激关于这个主题的一些建议,或者是否有更好的方法。

作为参考,我的 wp_nav_menu 脚本目前如下所示:

function html5blank_nav() {
    wp_nav_menu(
    array(
        'theme_location'  => 'header-menu',
        'menu'            => '',
        'container'       => 'div',
        'container_class' => 'menu-{menu slug}-container',
        'container_id'    => '',
        'menu_class'      => 'menu',
        'menu_id'         => '',
        'echo'            => true,
        'fallback_cb'     => 'wp_page_menu',
        'before'          => '',
        'after'           => '',
        'link_before'     => '',
        'link_after'      => '',
        'items_wrap'      => '<ul class="site-nav__list">%3$s</ul>',
        'depth'           => 0,
        'walker'          => ''
        )
    );
}

【问题讨论】:

    标签: php html wordpress bem


    【解决方案1】:

    关于步行者,你是对的。尝试这样的事情,然后将new Custom_Walker 传递给walker 提供给wp_nav_menu 的参数数组中的键

    <?php
    
    // functions.php
    // OR - preferrably - some file of it's own that you `include` by other means.
    
    class Custom_Walker extends \Walker_Nav_Menu
    {
        /**
         * Start the element output.
         *
         * The $args parameter holds additional values that may be used with the child
         * class methods. Includes the element output also.
         *
         * @since 2.1.0
         * @abstract
         *
         * @param string $output            Passed by reference. Used to append additional content.
         * @param object $object            The data object.
         * @param int    $depth             Depth of the item.
         * @param array  $args              An array of additional arguments.
         * @param int    $current_object_id ID of the current item.
         */
        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {    
            $anchorEl = sprintf(
                '<a class="site-nav__link%s" href=\'%s\'>%s</a>',
                ($item->object_id === get_the_ID()) ? ' site-nav__link--active' : '',
                $item->url,
                $item->title
            );
    
            $output .= sprintf( "\n<li class="site-nav__item">%s\n", $anchorEl);
        }//end start_el()
    
        /**
         * Ends the element output, if needed.
         *
         * The $args parameter holds additional values that may be used with the child class methods.
         *
         * @since 2.1.0
         * @abstract
         *
         * @param string $output Passed by reference. Used to append additional content.
         * @param object $object The data object.
         * @param int    $depth  Depth of the item.
         * @param array  $args   An array of additional arguments.
         */
        public function end_el(&$output, $object, $depth = 0, $args = array()) {   
            $output .= '</li>';
        }//end end_el()
    }//end class
    

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2014-01-12
      相关资源
      最近更新 更多