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:
<?php
// 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.