在 Wordpress 上添加一个预先存在的索引页面(根)作为新页面,这样您就可以在导航栏中 link 到 "Home"

Adding a pre-existing index page (root) as a new page on Wordpress so you can link to "Home" in navigation bar

这可能是一个非常愚蠢的问题,但我正在使用 word presses 功能来添加导航栏: $menuItems = array('theme_location' => 'primary'); wp_nav_menu($menuItems); 这样我就可以在控制面板中添加页面和订单。

我的主题文件夹的根目录中已经有一个索引页 (index.php)。我不知道如何将它添加为页面,以便它出现在导航栏中?该网站上的 URL 路径只是 localhost/wordpress/ 但在添加新页面时,您不能将永久链接留空。

有什么建议吗?提前致谢。

如果其他人遇到此问题,我已经找到了解决方案。

我从 Building a simple list 复制了函数,并在最后附加了我自己的 link ,就像这样

$homeUrl =  home_url();
$menuList .= '<li><a class="menu-item menu-item-type-post_type menu-item-object-page" href="'. $homeUrl .'">Home</a></li>';

完整代码为:

// Get the location of the navigation menu
$locations = get_nav_menu_locations();

// Get the menu itself from the location (primary is header in my case)
$menu = wp_get_nav_menu_object($locations['primary'] );

// Get the menu items
$menuItems = wp_get_nav_menu_items($menu->term_id);

// Initialise a string to hold the unorded list
$menuList = '<ul id="menu-' . $menuName . '">';

// Iterate through each menu item from the list of all menu items and append
// Name of menu item and url to its page
foreach ( (array) $menuItems as $key => $menuItem ) {
     $title = $menuItem->title;
     $url = $menuItem->url;
     $menuList .= '<li><a href="' . $url . '">' . $title . '</a></li>';
}
// Get the link to home url (in my case root (localhost/wordpress/)
$homeUrl =  home_url();

// Append own link
$menuList .= '<li><a href="'. $homeUrl .'">Home</a></li>';

// Close the unordered list tag
$menuList .= '</ul>';

// Echo the list back to html
echo $menuList;

这段代码包含在导航栏应该在的头文件中,包裹在 PHP 块中。

我的问题是使用 wp_nav_menu($menuItems); 意味着我需要添加的硬编码列表项未包含在函数自动创建的无序列表中,从而导致样式问题。 这将解压缩列表,附加自己的自定义 link,并显示它。