admin管理员组文章数量:1291840
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="/">About Us</a></li>
How can I achieve it?
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/about-us/">About Us</a></li>
How can I achieve it?
Share Improve this question edited Jan 29, 2016 at 22:05 Maqk 3983 silver badges13 bronze badges asked Jan 15, 2016 at 21:51 PikkPikk 2791 gold badge6 silver badges14 bronze badges 2- Worst case, you can always write you're own custom walker - codex.wordpress/Class_Reference/Walker – jgraup Commented Jan 15, 2016 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 Commented Jan 15, 2016 at 22:10
2 Answers
Reset to default 1To 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.
The change to be done is to change:
$item_output .= '<a'. $attributes .'>';
to
$item_output .= '<a itemprop=url'. $attributes .'>';
本文标签: navigationHow to add a parameter to ltagt links in the nav menu
版权声明:本文标题:navigation - How to add a parameter to <a> links in the nav menu? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741539818a2384253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论