编辑WordPress导航菜单漫游

时间:2018-02-07 作者:Orkhan Hasanli

我已将T5\\u Nav\\u Menu\\u Walker连接到我的WP网站。https://gist.github.com/thefuxia/1053467

我想自定义我的WP菜单,以便能够通过FontAwesome类从WP管理员添加图标。屏幕截图:Show

但当我在菜单中添加CSS类时,HTML代码中没有任何输出,因为我使用t5\\U nav\\U walker。如何在wordpress walker中添加图标?此walker的Php代码:

<?php # -*- coding: utf-8 -*-
/**
 * Create a nav menu with very basic markup.
 *
 * @author Thomas Scholz http://toscho.de
 * @version 1.0
 */
class menu_walker extends Walker_Nav_Menu
{
    /**
     * Start the element output.
     *
     * @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. May be used for padding.
     * @param  array $args    Additional strings.
     * @return void
     */
    public function start_el( &$output, $item, $depth, $args )
    {
        $output     .= \'<li>\';
        // $output .= \'<li\'.($item->current ? \' class="current"\':\'\').\'>\';
        $attributes  = \'class="app-menu__item"\';

        ! empty ( $item->attr_title )
            // Avoid redundant titles
            and $item->attr_title !== $item->title
            and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';

        ! empty ( $item->url )
            and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';



        $attributes  = trim( $attributes );
        $title       = apply_filters( \'the_title\', $item->title, $item->ID );
        $item_output = "$args->before<a $attributes>$args->link_before$title</a>"
                        . "$args->link_after$args->after";

        // Since $output is called by reference we don\'t need to return anything.
        $output .= apply_filters(
            \'walker_nav_menu_start_el\'
            ,   $item_output
            ,   $item
            ,   $depth
            ,   $args
        );
    }

    /**
     * @see Walker::start_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function start_lvl( &$output )
    {
        $output .= \'<ul class="treeview-menu">\';
    }

    /**
     * @see Walker::end_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function end_lvl( &$output )
    {
        $output .= \'</ul>\';
    }

    /**
     * @see Walker::end_el()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    function end_el( &$output )
    {
        $output .= \'</li>\';
    }
}

2 个回复
最合适的回答,由SO网友:Maxim Sarandi 整理而成

您需要为菜单项创建其他字段。因为如果您将使用类字段,您将阻止向链接添加其他类。但如果可以的话-创建<i class="$your_class"></i> 链接标题之前。“Dilip Gupta”给了你解决方案。

SO网友:Dilip Gupta

看,你需要更新你$item_output 变量fontawesome以这种格式输出图标<i class="fa fa-angle-right"></i>.

您需要添加此<i> 在您的<a> 像这样。

$item_output = "$args->before"
    . "<a $attributes>"
        . "$args->link_before"
        . "<i class=\'$variable_which_stores_fontawesomme_value\'></i>"
        . "$title"
    . "</a>"
    . "$args->link_after"
    . "$args->after";
$variable_which_stores_fontawesomme_value 应等于fa fa-angle-right.

结束