在您网站的导航菜单中添加图标可为访问者提供有关内容的视觉,并为您的网站添加漂亮的设计风格。

在下面的教程中,我将演示如何在不使用任何插件或上传任何图像的情况下完成此操作。 相反,您将通过WordPress管理员对导航菜单进行一些调整,然后在主题文件中添加一些代码。

The WordPress Toolbox
Unlimited Downloads: 500,000+ WordPress Themes, Plugins, Templates & Design Assets

教程:如何给wordpress菜单增加图标

DOWNLOAD NOW

完成后,导航菜单中的每个项目旁边都会显示一些简单的图标,最好的方法是您不必上传任何可能会降低网站速度的图像或文件。 这是我们完成后导航菜单的外观:

教程:如何给wordpress菜单增加图标

 

<?php
/**
 * Custom nav walker
 *
 * Custom nav walker to assign icons to menu items.
 */
class Iepress_Icon_Walker extends Walker_Nav_Menu
{
  /**
   * @see Walker::start_el()
   * @since 3.0.0
   *
   * @param string $output Passed by reference. Used to append additional content.
   * @param object $item Menu item data object.
   * @param int $depth Depth of menu item. Used for padding.
   * @param int $current_page Menu item ID.
   * @param object $args
   */
  function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    $class_names = $value = '';
    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $classes[] = 'menu-item-' . $item->ID;
    /** Remove icon class from list item */
    if ( $icon_class = preg_grep( '/fa-/', $classes ) )
      $classes = array_diff( $classes, $icon_class );
        
    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    $output .= $indent . '<li' . $id . $value . $class_names .'>';
    $atts = array();
    $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
    $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
    $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
    $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
    $attributes = '';
    foreach ( $atts as $attr => $value ) {
      if ( ! empty( $value ) ) {
        $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
        $attributes .= ' ' . $attr . '="' . $value . '"';
      }
    }
    $item_output = $args->before;
    $item_output .= '<a'. $attributes .'>';
    /** Add an icon if assigned. See check above */
    if ( $icon_class )
      $item_output .= '<i class="fa ' . current( $icon_class ) . '"></i>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;
    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  }
}

使用方法

<?php wp_nav_menu(array(
  'theme_location' => 'main-menu',
  'depth'          => 1,
  'container'      => '',
  'walker'     => new Iepress_Icon_Walker
)); ?>

然后我们通过常规的在菜单处css类的选项框处填写想要的图标后缀即可,如fa-home

注意:此教程需要您的主题集成了Font Awesome。如果没有集成,你可用把下面的代码复制到您的主题的functions.php文件内即可。

<?php
function iepress_enqueue_icon_stylesheet() {
  wp_register_style( 'fontawesome', 'http:////maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' );
  wp_enqueue_style( 'fontawesome');
}
add_action( 'wp_enqueue_scripts', 'iepress_enqueue_icon_stylesheet' );
?>

发表回复