在Wordpress上添加一个预先存在的索引页(根)作为一个新页面,这样你就可以链接到“首页”.在导航栏中


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

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

我已经有一个索引页(index.php)在我的主题文件夹的根。我不知道如何添加它作为一个页面,所以它会出现在导航栏?网址路径,它在网站上只是localhost/wordpress/,但当添加一个新的页面,你不能离开永久链接为空白。

提示吗?

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

我从构建一个简单的列表中复制了这个函数,并在末尾添加了我自己的链接,如下所示

$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);将意味着我需要添加的硬编码列表项不包括在函数自动创建的无序列表中,从而导致样式问题。这将解包列表,附加自己的自定义链接,并显示它。

相关文章: