1

I currently call menus with a basic wp_nav_menu code:

<?php wp_nav_menu( array('theme_location' => 'primary') ); ?>

I am trying to find the easiest way to add itemprop="url" to the line elements for the purpose of Schema.org markup. However, all of the codes I have found seem overly complex. Any help is greatly appreciated. Is there a way to simply impact the line elements without impacting others menus?

  • So you know a code that works, so what is the problem? There is no such thing as easy code, just people with more and less knowledge in a specific domain. – Mark Kaplun Sep 22 '15 at 18:07
2

To add attributes to the menu's li elements, you'd have to write your own custom walker that extends the default Walker_Nav_Menu class (which is itself an extension of the Walker class). For more info: https://codex.wordpress.org/Class_Reference/Walker

But as itemprop="url" should normally be added to anchor elements, you could use the nav_menu_link_attributes filter.

For example, as per the WP docs, adding this to your functions.php will add the attribute itemprop="url" to your anchor elements within the list item elements of your menu:

function add_menu_atts( $atts, $item, $args ) {
  $atts['itemprop'] = 'url';
  return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_menu_atts', 10, 3 );

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.