admin管理员组文章数量:1332394
Edit: after doing some research I've found that in the wp_nav_menu
function uses the following code to display the containers value.
if ( $args->container ) {
/**
* Filter the list of HTML tags that are valid for use as menu containers.
*
* @since 3.0.0
*
* @param array $tags The acceptable HTML tags for use as menu containers.
* Default is array containing 'div' and 'nav'.
*/
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
$show_container = true;
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
$nav_menu .= '<'. $args->container . $id . $class . '>';
}
}
lines 341 to 357 /
$nav_menu .= '<'. $args->container . $id . $class . '>';
The above line deals with the output of the container information I'm now trying to figure out how to modify this information within the custom walker class to add the extra fields.
End of Edit
I'm currently trying to add aria roles and schema markup to a custom walker I've created.
I'm trying to avoid wrapping the menu inside the tags directly like below. I would prefer to edit the container and attach the extra elements dynamically.
<nav class="" role="navigation" itemscope="itemscope" itemtype="">
wp_nav_menu( $defaults );
</nav>
Currently my wp_nav_menu call looks like this.
$defaults = array(
'menu' => '',
'menu_class' => '',
'menu_id' => '',
'container' => 'nav',
'container_class' => 'a-primarymenu',
'container_id' => '',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new Custom_Nav_Walker( 'a-primarymenu' ),
'theme_location' => 'primary',
'items_wrap' => '<ul>%3$s</ul>',
); wp_nav_menu( $defaults );
The container class I assign is passed to the custom walker class and allows my menu output to be as follows.
<nav class="a-primarymenu">
<ul>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
</ul>
</nav>
What I need help with is figuring out how to modifer the container html output
aka <nav class="a-primarymenu">
so I can add role="navigation"
and itemscope="itemscope" itemtype=""
to the element.
Which would then produce.
<nav class="a-primarymenu" role="navigation" itemscope="itemscope" itemtype="">
The closest match I could find to this desired output was however this is just a request to the wordpress core and not a modifiered walker.
Edit: after doing some research I've found that in the wp_nav_menu
function uses the following code to display the containers value.
if ( $args->container ) {
/**
* Filter the list of HTML tags that are valid for use as menu containers.
*
* @since 3.0.0
*
* @param array $tags The acceptable HTML tags for use as menu containers.
* Default is array containing 'div' and 'nav'.
*/
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
$show_container = true;
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
$nav_menu .= '<'. $args->container . $id . $class . '>';
}
}
lines 341 to 357 https://developer.wordpress/reference/functions/wp_nav_menu/
$nav_menu .= '<'. $args->container . $id . $class . '>';
The above line deals with the output of the container information I'm now trying to figure out how to modify this information within the custom walker class to add the extra fields.
End of Edit
I'm currently trying to add aria roles and schema markup to a custom walker I've created.
I'm trying to avoid wrapping the menu inside the tags directly like below. I would prefer to edit the container and attach the extra elements dynamically.
<nav class="" role="navigation" itemscope="itemscope" itemtype="http://schema/SiteNavigationElement">
wp_nav_menu( $defaults );
</nav>
Currently my wp_nav_menu call looks like this.
$defaults = array(
'menu' => '',
'menu_class' => '',
'menu_id' => '',
'container' => 'nav',
'container_class' => 'a-primarymenu',
'container_id' => '',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new Custom_Nav_Walker( 'a-primarymenu' ),
'theme_location' => 'primary',
'items_wrap' => '<ul>%3$s</ul>',
); wp_nav_menu( $defaults );
The container class I assign is passed to the custom walker class and allows my menu output to be as follows.
<nav class="a-primarymenu">
<ul>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
<li class="a-primarymenu__item"><a href="#link">Link</a></li>
</ul>
</nav>
What I need help with is figuring out how to modifer the container html output
aka <nav class="a-primarymenu">
so I can add role="navigation"
and itemscope="itemscope" itemtype="http://schema/SiteNavigationElement"
to the element.
Which would then produce.
<nav class="a-primarymenu" role="navigation" itemscope="itemscope" itemtype="http://schema/SiteNavigationElement">
The closest match I could find to this desired output was https://core.trac.wordpress/ticket/35127 however this is just a request to the wordpress core and not a modifiered walker.
Share Improve this question edited May 9, 2016 at 6:29 550 asked May 8, 2016 at 13:20 550550 451 gold badge1 silver badge8 bronze badges 2 |1 Answer
Reset to default 1@Dan meant this:
<nav role="navigation" itemscope="itemscope" itemtype="http://schema/SiteNavigationElement"><?php
wp_nav_menu([
'container' => '',
'items_wrap' => '<ul>%3$s</ul>',
...
]);
?></nav>
本文标签: phpHow to add aria role and schema markup to custom walker container
版权声明:本文标题:php - How to add aria role and schema markup to custom walker container 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742281942a2446255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
container
=>false
and wrapul
list inside yournav
element. – James Vu Commented May 8, 2016 at 14:05