2

I have to add itemprop="url" to the links of the links in the navbar of this site.

In the settings I found the function wp_nav_menu(). Even after reading the WP codex, I am not able to make the necessary changes by myself.

Here is the code:

<!-- 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 -->

And here is the Walker class:

/*
 * 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 );
    }
}
}

So the final links will look like this:

<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>

How can I achieve it?

  • Worst case, you can always write you're own custom walker - codex.wordpress.org/Class_Reference/Walker – jgraup Jan 15 '16 at 22:02
  • I think there is a Walker already. I just added it above. I just don't know what to edit there to add that parameter. – Pikk Jan 15 '16 at 22:10
0

The change to be done is to change:

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

to

$item_output .= '<a itemprop=url'. $attributes .'>';
  • 1
    Why this -1 ? It works fine this solution. – Pikk Jan 17 '16 at 10:46
  • I didn't downvote, but it's probably because it isn't the cleanest solution. – Pim Sep 28 '18 at 14:06
0

To add a new attribute to your anchor link for the nav items concatenate $attribute variable with your required attribute. You have commended code just uncomment the code and use those

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

Hope this works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.