如何向导航菜单中的<a>链接添加参数?

时间:2016-01-15 作者:Pikk

我必须补充itemprop="url" 导航栏中链接的链接this site.

在设置中,我找到了功能wp_nav_menu(). 即使在阅读了WP codex之后,我也无法自己进行必要的更改。

代码如下:

<!-- BEGIN MAIN NAVIGATION -->
<nav class="nav nav__primary clearfix">
<?php if (has_nav_menu(\'header_menu\')) {
    wp_nav_menu( array(
        \'container\'      => \'ul\',
        \'menu_class\'     => \'sf-menu\',
        \'menu_id\'        => \'topnav\',
        \'depth\'          => 0,
        \'theme_location\' => \'header_menu\',
        \'walker\'         => new description_walker()
    ));
} else {
    echo \'<ul class="sf-menu">\';
        $ex_page = get_page_by_title( \'Privacy Policy\' );
        if ($ex_page === NULL) {
            $ex_page_id = \'\';
        } else {
            $ex_page_id = $ex_page->ID;
        }
        wp_list_pages( array(
            \'depth\'    => 0,
            \'title_li\' => \'\',
            \'exclude\'  => $ex_page_id
            )
        );
    echo \'</ul>\';
} ?>
</nav><!-- END MAIN NAVIGATION -->
这是Walker课程:

/*
 * Navigation with description
 *
 */
if (! class_exists(\'description_walker\')) {
    class description_walker extends Walker_Nav_Menu {
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';

        $class_names = $value = \'\';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
        $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';

        $output .= $indent . \'<li id="menu-item-\'. $item->ID . \'"\' . $value . $class_names .\'>\';

        // $attributes  = ! empty( $item->attr_title ) ? \' title="\'  . esc_attr( $item->attr_title ) .\'"\' : \'\';
        // $attributes .= ! empty( $item->target )     ? \' target="\' . esc_attr( $item->target     ) .\'"\' : \'\';
        // $attributes .= ! empty( $item->xfn )        ? \' rel="\'    . esc_attr( $item->xfn        ) .\'"\' : \'\';
        // $attributes .= ! empty( $item->url )        ? \' href="\'   . esc_attr( $item->url        ) .\'"\' : \'\';

        $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        : \'\';

        /**
         * Filter the HTML attributes applied to a menu item\'s <a>.
         *
         * @since 3.6.0
         *
         * @see wp_nav_menu()
         *
         * @param array $atts {
         *     The HTML attributes applied to the menu item\'s <a>, empty strings are ignored.
         *
         *     @type string $title  Title attribute.
         *     @type string $target Target attribute.
         *     @type string $rel    The rel attribute.
         *     @type string $href   The href attribute.
         * }
         * @param object $item The current menu item.
         * @param array  $args An array of wp_nav_menu() arguments.
         */
        $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 . \'"\';
            }
        }

        $description  = ! empty( $item->description ) ? \'<span class="desc">\'.esc_attr( $item->description ).\'</span>\' : \'\';

        if($depth != 0) {
            $description = $append = $prepend = "";
        }

        $item_output = $args->before;
        $item_output .= \'<a\'. $attributes .\'>\';
        $item_output .= $args->link_before;

        if (isset($prepend))
            $item_output .= $prepend;

        $item_output .= apply_filters( \'the_title\', $item->title, $item->ID );

        if (isset($append))
            $item_output .= $append;

        $item_output .= $description.$args->link_after;
        $item_output .= \'</a>\';
        $item_output .= $args->after;

        $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
    }
}
}
最后的链接如下所示:

<li id="menu-item-2005" class="menu-item menu-item-type-post_type menu-item-object-page"><a itemprop="url" title="thetitle" href="http://thesite.com/about-us/">About Us</a></li>
我怎样才能做到这一点?

2 个回复
SO网友:Maqk

要向导航项目的锚定链接添加新属性,请将$attribute变量与所需属性连接起来。您推荐的代码只需取消对代码的注释并使用

     $attributes  = ! empty( $item->attr_title ) ? \' title="\'  . esc_atta( $item->attr_title ) .\'"\' : \'\';
     $attributes .= ! empty( $item->target )     ? \' target="\' . esc_atto( $item->target     ) .\'"\' : \'\';
     $attributes .= ! empty( $item->xfn )        ? \' rel="\'    . esc_attr( $item->xfn        ) .\'"\' : \'\';
     $attributes .= ! empty( $item->url )        ? \' href="\'   . esc_attr( $item->url        ) .\'"\' : \'\';
     $attributes .= ! empty( $item->url )        ? \' itemprop="\'   . esc_attr( $item->url        ) .\'"\' : \'\';

    //$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        : \'\';
希望这对你有用。

SO网友:Pikk

要做的更改是更改:

$item_output .= \'<a\'. $attributes .\'>\';

$item_output .= \'<a itemprop=url\'. $attributes .\'>\';

相关推荐

无法将设置添加到“NAV_MENUS”定制器面板

我试图在自定义程序的“菜单”面板中添加一个复选框,但由于某些原因,它没有显示出来。如果我尝试将其从“nav\\u菜单”更改为“title\\u tagline”或“colors”,复选框会显示得很好。是什么阻止它显示在“菜单”面板上?// add custom options to the Customizer function nssra_customizer_options($wp_customize) { // add \"menu primary flex\" checkb