Custom theme navigation 时间:2015-12-20 作者:Henry Lynx 我正在从头开始创建一个主题,但在理解如何链接导航中的页面时遇到了一个问题。这些页面是用纯html编写的。我有“关于我”、“联系人”、“博客”等。。。我了解我可以使用:<li class="" id="nava"><a href="<?php echo get_permalink(1); ?>">Home</a></li> 在wordpress中获得此类功能的最佳机制是什么?如果你知道一个关于wordpress主题开发初学者的好教程,我可以好好利用它。谢谢 1 个回复 最合适的回答,由SO网友:N00b 整理而成 1. Register your menu使用注册菜单register_nav_menus() 之后,您可以从管理区域选择该菜单:外观->菜单。将其中一个添加到functions.php:<?php register_nav_menus( array( \'primary\' => __( \'Main Menu\', \'tt\' ) ) ); //\'primary\' is basically like a slug that can be used to get that specific menu later on //\'Main menu\' is the name that appears in admin area: Appearance -> Menus //\'tt\' is your translation creds //If you\'re not making your strings translatable: register_nav_menus( array( \'primary\' => \'Main Menu\' ) ); ?> 2. Echo your menu使用wp_nav_menu() 要将此菜单插入任意位置,请在您的案例中插入标题。我在下面添加了参考资料,您可以从中了解不同的参数,但我将对重要参数添加注释:<?php $defaults = array( \'theme_location\' => \'primary\', //Same slug as you used to register it \'menu\' => \'\', \'container\' => \'div\', \'container_class\' => \'\', \'container_id\' => \'\', \'menu_class\' => \'menu\', \'menu_id\' => \'\', \'echo\' => true, \'fallback_cb\' => \'wp_page_menu\', \'before\' => \'\', \'after\' => \'\', \'link_before\' => \'\', \'link_after\' => \'\', \'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\', \'depth\' => 0, //How many submenus: 0 if only main, -1 if all \'walker\' => \'\' ); wp_nav_menu( $defaults ); //If you want to keep everything default, you only need to add your menu slug: wp_nav_menu( array( \'theme_location\' => \'primary\' ) ); //Just make sure to put wp_nav_menu() where you want your menu to appear ?> 3. Make your menu不要忘记从管理菜单中构建/添加菜单:Appearance -> Menus 并确保选择在第一步中注册的一个。您可以自定义、更改、编辑和添加任何您需要的内容,它将自动更新您的菜单。<小时>References:register_nav_menus(): https://codex.wordpress.org/Function_Reference/register_nav_menuwp_nav_menu(): https://codex.wordpress.org/Function_Reference/wp_nav_menu 文章导航