如何将模式标记添加到WordPress菜单功能

时间:2015-10-09 作者:user3088202

我想添加到我的主要导航项目,似乎唯一的方法是更改菜单功能。以下是我正在尝试实施的结构:

<ul itemscope itemtype="http://www.schema.org/SiteNavigationElement">
  <li itemprop="name"><a itemprop="url" href="domain.com">Link</a></li>
</ul>
以下是生成主菜单的函数:

   // Output Primary Navigation
    // =============================================================================

    if ( ! function_exists( \'x_output_primary_navigation\' ) ) :
      function x_output_primary_navigation() {

        if ( x_is_one_page_navigation() ) {

          wp_nav_menu( array(
            \'menu\'           => x_get_one_page_navigation_menu(),
            \'theme_location\' => \'primary\',
            \'container\'      => false,
            \'menu_class\'     => \'x-nav x-nav-scrollspy\',
            \'link_before\'    => \'<span>\',
            \'link_after\'     => \'</span>\'
          ) );

        } elseif ( has_nav_menu( \'primary\' ) ) {

          wp_nav_menu( array(
            \'theme_location\' => \'primary\',
            \'container\'      => false,
            \'menu_class\'     => \'x-nav\',
            \'link_before\'    => \'<span>\',
            \'link_after\'     => \'</span>\'
          ) );

        } else {

          echo \'<ul class="x-nav"><li><a href="\' . home_url( \'/\' ) . \'wp-admin/nav-menus.php">Assign a Menu</a></li></ul>\';

        }

      }
    endif;

1 个回复
SO网友:s_ha_dum

要获得这种控制,您可能需要创建一个自定义助行器。查看的默认值wp_nav_menu()

$defaults = array(
    \'theme_location\'  => \'\',
    \'menu\'            => \'\',
    \'container\'       => \'div\',
    \'container_class\' => \'\',
    \'container_id\'    => \'\',
    \'menu_class\'      => \'menu\',
    \'menu_id\'         => \'\',
    \'echo\'            => true,
    \'fallback_cb\'     => \'wp_page_menu\',
    \'before\'          => \'\',
    \'after\'           => \'\',
    \'link_before\'     => \'\',
    \'link_after\'      => \'\',
    \'items_wrap\'      => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
    \'depth\'           => 0,
    \'walker\'          => \'\'
);
您需要创建一个新的助行器,并在其中加载\'walker\' => \'\' 论点有an example toward the middle of the page, 如果你搜索的话,这里有很多Q/A。

由于看起来您正在破解一个主题,我建议创建一个儿童主题,并通过wp_nav_menu_args filter.

$args = apply_filters( \'wp_nav_menu_args\', $args );