• Hello,

    I have the following:

    <nav class="navbar navbar-default navbar-fixed-top" id="menu-nav" role="navigation">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
      </div>
      <!-- Collect the nav links, forms, and other content for toggling -->
        <?php wp_nav_menu( array(
          'sort_column'     => 'menu_order',
          'theme_location'  => 'header-menu',
          'items_wrap'      => '<ul class="menu-nav navbar-nav scroll-menu">%3$s</ul>',
          'container_class' => 'collapse navbar-collapse menu-header '
        ) ); ?>
      </nav>

    I understand that temscope itemtype="https://schema.org/SiteNavigationElement" should go to the <nav>. That’s fine.

    However how can I add
    itemprop="url" to the of that navbar??

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can create a new walker class filter and asign it to your wp_nav_menu function.

    Example:

    In your functions.php file add:

    class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth) {
      $indent = str_repeat("\t", $depth);
      $output .= "\n$indent<ul class=\"menu-nav navbar-nav scroll-menu\">\n";
    }
    function start_el(&$output, $item, $depth, $args) {
      $output.= '<li>';
      $attributes = ' itemprop="url" target="_blank" href="' .esc_attr($item->url). '" title="' .esc_attr($item->attr_title). '"';
      $item_output = $args->before;
      $current_url = (is_ssl()?'https://':'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
      $item_url = esc_attr( $item->url );
      $item_output.= '<a'. $attributes .'>'.$item->title.'</a>';
      $item_output.= $args->after;
      $output.= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
    }

    Now add your walker filter to your wp_nav_menu:

    <?php wp_nav_menu( array(
          'sort_column'     => 'menu_order',
          'theme_location'  => 'header-menu',
          'items_wrap'      => '<ul class="menu-nav navbar-nav scroll-menu">%3$s</ul>',
          'container_class' => 'collapse navbar-collapse menu-header ',
          'walker' => new My_Walker_Nav_Menu(),
        ) ); ?>

    Source: https://webformyself.com/forum/index.php?showtopic=2887

    You can create a new walker class filter and assign it to your wp_nav_menu function.

    Example:

    In your functions.php file add:

    class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth) {
      $indent = str_repeat("\t", $depth);
      $output .= "\n$indent<ul class=\"menu-nav navbar-nav scroll-menu\">\n";
    }
    function start_el(&$output, $item, $depth, $args) {
      $output.= '<li>';
      $attributes = ' itemprop="url" target="_blank" href="' .esc_attr($item->url). '" title="' .esc_attr($item->attr_title). '"';
      $item_output = $args->before;
      $current_url = (is_ssl()?'https://':'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
      $item_url = esc_attr( $item->url );
      $item_output.= '<a'. $attributes .'>'.$item->title.'</a>';
      $item_output.= $args->after;
      $output.= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
    }

    Now add your walker filter to your wp_nav_menu:

    <?php wp_nav_menu( array(
          'sort_column'     => 'menu_order',
          'theme_location'  => 'header-menu',
          'items_wrap'      => '<ul class="menu-nav navbar-nav scroll-menu">%3$s</ul>',
          'container_class' => 'collapse navbar-collapse menu-header ',
          'walker' => new My_Walker_Nav_Menu(),
        ) ); ?>

    Source: https://webformyself.com/forum/index.php?showtopic=2887

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add schema.org SiteNavigationElement and property URL to navbar?’ is closed to new replies.