Support » Fixing WordPress » Adding index (root) as a page for navigation bar

  • Resolved alifhughes


    This might be a really stupid question but I am using word presses function to add a navigation bar:
    $menuItems = array('theme_location' => 'primary');

    I already have an index page (index.php) in the root of my themes folder. I can’t work out how to add it as a page so it will appear in the navigation bar? The URL path to it on the site is just localhoset/wordpress/ but when adding a new page you can’t leave the permalink as blank.

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • just add a custom link to your menu.

    Do you mean in the actual HTML code? I have tried but my custom styling and JavaScrip doesn’t seem to like the combination of both.

    I have found a solution if anyone else is having problems with this.

    I copied the function from Building a simple list and appended my own link to it at the end like so

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

    The full code is:

    // 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;

    The problem in my case is that using wp_nav_menu($menuItems); would mean that the hardcoded list item I needed to add wasn’t included in the unordered list that the function automatically created, thus causing styling problems.
    This unpacks the list, appends own custom link, and displays it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding index (root) as a page for navigation bar’ is closed to new replies.