如何更改WordPress菜单结构(ul > li到nav > a)

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

How to Change WordPress Menu Structure (ul > li to nav > a)

问题

我正在尝试创建WordPress菜单使用:

  1. wp_nav_menu(
  2. array(
  3. 'menu' => '顶部菜单',
  4. 'container' => 'nav',
  5. 'container_class' => '',
  6. 'theme_location' => '顶部菜单',
  7. 'items_wrap' => '<nav>%3$s</nav>'
  8. )
  9. );

问题是WordPress向导航中添加了ui > ul结构,而我想使用以下结构:

  1. <nav>
  2. <a href="page1.html">页面1</a>
  3. <a href="page2.html">页面2</a>
  4. <a href="page3.html">页面3</a>
  5. <a href="page4.html">页面4</a>
  6. </nav>

有没有想法如何实现这样的结构,以便我可以用无序/有序列表以外的任何内容来组织菜单?

英文:

I'm trying to create a WordPress menu using:

  1. &lt;?php
  2. wp_nav_menu(
  3. array(
  4. &#39;menu&#39; =&gt; &#39;Top Mid&#39;,
  5. &#39;container&#39; =&gt; &#39;nav&#39;,
  6. &#39;container_class&#39; =&gt; &#39;&#39;,
  7. &#39;theme_location&#39; =&gt; &#39;Top Mid&#39;,
  8. &#39;items_wrap&#39; =&gt; &#39;&lt;ul&gt;%3$s&lt;/ul&gt;&#39;
  9. )
  10. );
  11. ?&gt;

The problem is that WordPress adding a ui > ul structure to the navigation whereas I want to use the following structure:

  1. &lt;nav&gt;
  2. &lt;a href=&quot;page1.html&quot;&gt;Page 1&lt;/a&gt;
  3. &lt;a href=&quot;page2.html&quot;&gt;Page 2&lt;/a&gt;
  4. &lt;a href=&quot;page3.html&quot;&gt;Page 3&lt;/a&gt;
  5. &lt;a href=&quot;page4.html&quot;&gt;Page 4&lt;/a&gt;
  6. &lt;/nav&gt;

Any thoughts on how this can be implemented so that I can structure the menu with anything else than the unordered/ordered list?

答案1

得分: 1

你可以使用此函数。这会添加<a>标签。

  1. $navItems = array(
  2. 'container' => false,
  3. 'echo' => false,
  4. 'items_wrap' => '%3$s',
  5. 'depth' => 0,
  6. );
  7. echo "<nav>";
  8. echo strip_tags(wp_nav_menu($navItems), '<a>');
  9. echo "</nav>";
英文:

You can use this function. This adds &lt;a&gt; tag.

  1. $navItems = array(
  2. &#39;container&#39; =&gt; false,
  3. &#39;echo&#39; =&gt; false,
  4. &#39;items_wrap&#39; =&gt; &#39;%3$s&#39;,
  5. &#39;depth&#39; =&gt; 0,
  6. );
  7. echo &quot;&lt;nav&gt;&quot;;
  8. echo strip_tags(wp_nav_menu($navItems), &#39;&lt;a&gt;&#39;);
  9. echo &quot;&lt;/nav&gt;&quot;;

huangapple
  • 本文由 发表于 2023年4月17日 22:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035995.html
匿名

发表评论

匿名网友

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

确定