自定义 Walker 显示顶层项目名称

huangapple go评论67阅读模式
英文:

Custom Walker Show Top Level Item Name

问题

I am using the following custom NAV Walker to output a custom submenu. I would like to include the parent title in the walker but it's just showing up blank.

class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
    
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat( "\t", $depth );
        $parent_id = $this->db_fields['id'][ $depth - 1 ];
        $parent_item = get_post( $parent_id );
        $parent_title = $parent_item->post_title;
        
		$output .= "\n$indent<p class=\"menu-title\">$parent_title</p>\n";
        $output .= "\n$indent<ul class=\"sub-menu\">\n";
    }
    
    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat( "\t", $depth );
        $output .= "$indent</ul>\n";
    }
}

wp_nav_menu( array(
	'theme_location'    => 'primary',
	'menu_class'     => 'nav-menu',
	'depth'             => 2,
	'container'         => 'div',
	'container_class'   => '',
	'container_id'      => 'main-navigation',
	'walker' 			=> new Custom_Walker_Nav_Menu(),
) );
英文:

I am using the following custom NAV Walker to output a custom submenu. I would like to include the parent title in the walker but it's just showing up blank.

class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
    
    function start_lvl( &amp;$output, $depth = 0, $args = array() ) {
        $indent = str_repeat( &quot;\t&quot;, $depth );
        $parent_id = $this-&gt;db_fields[&#39;id&#39;][ $depth - 1 ];
        $parent_item = get_post( $parent_id );
        $parent_title = $parent_item-&gt;post_title;
        
		$output .= &quot;\n$indent&lt;p class=\&quot;menu-title\&quot;&gt;$parent_title&lt;/p&gt;\n&quot;;
        $output .= &quot;\n$indent&lt;ul class=\&quot;sub-menu\&quot;&gt;\n&quot;;
    }
    
    function end_lvl( &amp;$output, $depth = 0, $args = array() ) {
        $indent = str_repeat( &quot;\t&quot;, $depth );
        $output .= &quot;$indent&lt;/ul&gt;\n&quot;;
    }
}

wp_nav_menu( array(
	&#39;theme_location&#39;    =&gt; &#39;primary&#39;,
	&#39;menu_class&#39;     =&gt; &#39;nav-menu&#39;,
	&#39;depth&#39;             =&gt; 2,
	&#39;container&#39;         =&gt; &#39;div&#39;,
	&#39;container_class&#39;   =&gt; &#39;&#39;,
	&#39;container_id&#39;      =&gt; &#39;main-navigation&#39;,
	&#39;walker&#39; 			=&gt; new Custom_Walker_Nav_Menup(),
) );

答案1

得分: 0

我决定使用 jQuery,在需要的地方包含父标题。当用户悬停在菜单项上时,我使用 jQuery 将文本复制到目标元素中。

$(document).ready(function () {
	$('#main-navigation li.menu-item-has-children > a').hover(function () {
		// 在悬停时,从源元素获取文本
		var sourceText = $(this).text();
		// 用源文本替换目标元素中的文本
		$('.mega-menu .header-title').text(sourceText);
	});
})
英文:

I decided to use jQuery to include the parent title where I needed it. When the user hovers over the menu item, I use jQuery to copy the text to the target element.

$(document).ready(function () {
	$(&#39;#main-navigation li.menu-item-has-children &gt; a&#39;).hover(function () {
		// On hover, get text from source element
		var sourceText = $(this).text();
		// Replace text in target element with source text
		$(&#39;.mega-menu .header-title&#39;).text(sourceText);
	});
})

huangapple
  • 本文由 发表于 2023年5月11日 02:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定