Wordpress 自定义主题 - 在 footer.php 中调用时不显示菜单 [英] Wordpress custom theme - menu not showing when called in footer.php

查看:30
本文介绍了Wordpress 自定义主题 - 在 footer.php 中调用时不显示菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Wordpress 创建自定义网站主题,但遇到了一些问题.我在网站的页眉和页脚部分使用了两个完全相同的菜单.

I'm trying to create custom website theme for Wordpress and I ran into a bit of a problem. I use two absolutely same menus in my header and footer part of website.

打电话

wp_nav_menu(array('theme_location' => 'header'));

在 header.php 中运行良好,菜单打印出来没有任何问题,但如果我在我的 footer.php 文件中做同样的事情,菜单不会打印并且 var_dump(wp_nav_menu(array('theme_location'=> 'header'))); 打印错误.

in header.php works well, the menu prints out without any problem, but if I do the same in my footer.php file, the menu doesn't print and var_dump(wp_nav_menu(array('theme_location' => 'header'))); prints false.

我尝试了一些在 Google 上找到的解决方法来修改 functions.php 文件,但没有一个能帮助我解决该问题.我的functions.php文件现在只包含一行

I've tried some workarounds that I found on Google with modifying the functions.php file, but none of them helped me resolve that problem. My functions.php file now consists of only one line

register_nav_menus( array( 'header' => 'Header menu', 'footer' => 'Footer menu' ) );

是的,我尝试使用

wp_nav_menu(array('theme_location' => 'footer'))

同样,结果相同.如果我从 header.php 调用相同的函数

as well, with same result. If I call the same function from header.php

wp_nav_menu(array('theme_location' => 'footer'))

菜单效果很好.

推荐答案

您已正确注册了两个导航菜单.我总是在与 after_setup_theme 挂钩的初始主题设置挂钩中执行此操作.所以我会在你的functions.php中做这样的事情:

You have registered you two nav menus correctly. I always do that within my initial theme setup hook that gets hooked to the after_setup_theme hook. So I would do something like this in your functions.php:

function pietergoosen_theme_setup() {
  register_nav_menus( array( 
    'header' => 'Header menu', 
    'footer' => 'Footer menu' 
  ) );
 }

add_action( 'after_setup_theme', 'pietergoosen_theme_setup' );

请记住,您不必这样做.以下也有效

Keep in mind, you don't have to do it this way. The following also works

register_nav_menus( array( 
        'header' => 'Header menu', 
        'footer' => 'Footer menu' 
      ) );

您现在应该在外观>菜单>管理位置"下的后端看到两个菜单(仅当菜单存在时)

You should now see the two menus in the backend under "Appearance > Menus > Manage Locations" (Only if a menu exist)

为了页脚菜单,在页脚需要显示菜单的地方添加以下代码:

For the sake of the footer menu, add the following code in your footer where you need to display the menu:

<nav id="footer-navigation" class="site-navigation footer-navigation" role="navigation">
       <?php wp_nav_menu( array( 'theme_location' => 'footer', 'menu_class' => 'nav-menu', 'fallback_cb' => false ) ); ?>
</nav>

在这个阶段什么都不会显示,我认为这也是你卡住的地方.这样做的原因是没有任何项目分配给菜单,如果没有分配给菜单,则不会显示任何内容.所以我们必须插入一些东西来显示.

At this stage nothing will be displayed, and I think this is where you also get stuck at. The reason for this is that there aren't any items assigned to the menu, and if there are nothing assigned to a menu, then nothing will be displayed. So we have to insert something to be displayed.

在后端,转到外观 > 菜单 > 编辑菜单".在菜单名称"字段中,输入菜单名称,然后单击创建菜单".您现在可以在菜单屏幕中添加菜单.

In the backend, go to "Appearance > Menus > Edit Menus". In the "Menu Name" field, enter a name for your menu and click "Create Menu". You will now be able to add the menu in the menu screen.

您现在可以从左侧选择项目以插入到您的菜单中.您还可以设置菜单的位置,在这种情况下是在页脚中.我选择在页脚中显示类别.完成后点击保存菜单".

You can now choose items from the left hand side to insert into your menu. You can also set the location of the menu, in this case in the footer. I've selected to display the categories in the footer. Click "Save Menu" when done.

您现在应该会在前端看到您的导航菜单.

You should now see your nav menu in the front end.

您现在只需为导航栏添加样式即可.您将对标题导航菜单执行完全相同的操作,接受您将调用添加到 header.php 中的菜单.我希望你觉得这很有用.

You just have to add styling to your nav bar now. You will do exactly the same for the header nav menu, accept you will add the call to the menu in the header.php. I hope you find this usefull.

这篇关于Wordpress 自定义主题 - 在 footer.php 中调用时不显示菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆