我正在为一个客户做一个定制项目,在添加时遇到了一个问题data-scroll
在WordPress中的链接之前。我希望输出为<a data-scroll href=\'#\'>
.
数据滚动是我想在a之后添加的内容,这里href ="#"
希望有人能帮我。
我正在为一个客户做一个定制项目,在添加时遇到了一个问题data-scroll
在WordPress中的链接之前。我希望输出为<a data-scroll href=\'#\'>
.
数据滚动是我想在a之后添加的内容,这里href ="#"
希望有人能帮我。
自定义Walker是将任何内容添加到wp\\U nav\\U菜单的最佳方式
您必须在功能中保留以下内容。php
<?php
class MV_Cleaner_Walker_Nav_Menu extends Walker {
var $tree_type = array( \'post_type\', \'taxonomy\', \'custom\' );
var $db_fields = array( \'parent\' => \'menu_item_parent\', \'id\' => \'db_id\' );
function start_lvl(&$output, $depth) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent<ul class=\\"sub-menu\\">\\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\\t", $depth);
$output .= "$indent</ul>\\n";
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
$class_names = $value = \'\';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes = in_array( \'current-menu-item\', $classes ) ? array( \'current-menu-item\' ) : array();
$class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args ) );
$class_names = strlen( trim( $class_names ) ) > 0 ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';
$id = apply_filters( \'nav_menu_item_id\', \'\', $item, $args );
$id = strlen( $id ) ? \' id="\' . esc_attr( $id ) . \'"\' : \'\';
$output .= $indent . \'<li\' . $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 ) .\'"\' : \'\';
$item_output = $args->before;
$item_output .= \'<a data-scroll\'. $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$item_output .= \'</a>\';
$item_output .= $args->after;
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
function end_el(&$output, $item, $depth) {
$output .= "</li>\\n";
}
}
?>
然后你可以通过<?php wp_nav_menu( array( \'menu\'=>\'Footer Menu 1\',
menu_class => \'menu vertical footer-menu\',
\'walker\' => new MV_Cleaner_Walker_Nav_Menu() ) ); ?>
您需要使用以下筛选器:
示例:function filter_nav_menu_link_attributes($atts, $item, $args) {
if ( isset($args->theme_location) && $args->theme_location === \'my-menu-location\' ) {
$atts[\'data-scroll\'] = \'some-value\';
}
return $atts;
}
add_filter(\'nav_menu_link_attributes\', \'filter_nav_menu_link_attributes\', 10, 3);
使用$item
和$args
隔离并操作要在其中添加属性的特定菜单或特定菜单项。请在此处阅读更多信息:nav_menu_link_attributes
您需要在函数上添加此代码。当前主题的php文件。
add_action( \'wp_footer\', \'add_attr_nav_menu_link\' );
function add_attr_nav_menu_link(){
?>
<script type="text/javascript">
jQuery( document ).ready(function() {
jQuery(\'.nav-menu ul.menu li a\').each(function() {
jQuery(".nav-menu ul.menu li a").attr(\'data-scroll\', \'\');
});
});
</script>
<?php
}
如果您有任何疑问,请告诉我!!!!我试图在自定义程序的“菜单”面板中添加一个复选框,但由于某些原因,它没有显示出来。如果我尝试将其从“nav\\u菜单”更改为“title\\u tagline”或“colors”,复选框会显示得很好。是什么阻止它显示在“菜单”面板上?// add custom options to the Customizer function nssra_customizer_options($wp_customize) { // add \"menu primary flex\" checkb