如何使用WP_NAV_MENU在WordPress中显示自定义链接?

发布于 2025-01-27 19:40:38 字数 688 浏览 1 评论 0原文

我正在创建一个一页自定义主题。我正在努力使用自定义菜单,因为我想显示自定义链接。

我已经在function.php文件中注册了菜单

function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),          
     )
   );
 }
 add_action( 'init', 'register_my_menus' );

,然后我在header.php文件中显示了这样的菜单:

 <?php wp_nav_menu( array(
         'theme_location' => 'top-menu',
         'container' => 'ul',
         'menu_class'=> 'd-flex main-menu'
          ) );
    ?>

我已经为ul标记添加了自定义类,这还可以。导航有效,但仅适用于页面。当我创建一个页面时,该页面的链接显示在导航中。但是,创建自定义链接后,它们不会出现在导航中。

我正在搜索Stachoverflow,以搜索一些安静的时间,但没有成功。如何解决描述的问题?你有主意吗?

谢谢你的宝贵时间。

I'm creating an one-page custom theme. I'm struggling with a custom menu, because I want to display custom links.

I've registered menu in functions.php file

function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),          
     )
   );
 }
 add_action( 'init', 'register_my_menus' );

Then, I've displayed in header.php file like this:

 <?php wp_nav_menu( array(
         'theme_location' => 'top-menu',
         'container' => 'ul',
         'menu_class'=> 'd-flex main-menu'
          ) );
    ?>

I've added custom classes for ul tag, and that is okay. Navigation works but just for pages. When I create a page, a link for that page shows up in navigation. However, after creating custom links they don't appear in navigation.

I'm searching the Stachoverflow for some quiet time, but without success. How to resolve a described issue? Do you have an idea?

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2025-02-03 19:40:38

您的注册标题菜单带有标头键,并试图通过Top-Menu的钥匙将其获取,这是不可能的。您必须使用标头键键添加并获取标头菜单。我们已经更新了代码。您可以使用此代码。

<?php 
wp_nav_menu( array(
     'theme_location' => 'header-menu',
     'container' => 'ul',
     'menu_class'=> 'd-flex main-menu'
    ) ); 
?>

You have register header menu with header-menu key and trying to get it by key of top-menu which is not possible. You have to use the header-menu key to add and get a header menu. we have updated the code. you can use this code.

<?php 
wp_nav_menu( array(
     'theme_location' => 'header-menu',
     'container' => 'ul',
     'menu_class'=> 'd-flex main-menu'
    ) ); 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文