{"id":120,"date":"2016-07-15T22:07:44","date_gmt":"2016-07-15T22:07:44","guid":{"rendered":"https:\/\/alexrusin.com\/?p=120"},"modified":"2019-04-15T23:44:03","modified_gmt":"2019-04-15T23:44:03","slug":"fixing-menu-issue-on-wordpress-single-page-themes","status":"publish","type":"post","link":"https:\/\/blog.alexrusin.com\/fixing-menu-issue-on-wordpress-single-page-themes\/","title":{"rendered":"Fixing Menu Issue on WordPress Single-Page Themes"},"content":{"rendered":"
Among WordPress themes out there, there are so called single- or one-page themes. Most of the content of the page is located on one page. Menu links are route to different sections of the page via anchors tags. The menu is created using custom anchor links like so:<\/p>\n
<\/p>\n
The corresponding part of an anchor tag looks like this <a name=”#resume”><\/a><\/strong><\/p>\n This anchor link only works if you are jumping to a specific spot on the same page. For instance, if you have a blog page different from your home page unless you put the full URL of the home page like so http:\/\/www.mywebsite.com#resume. <\/strong>So, not a big deal, let’s replace Custom Link URL on the menu page with the full URL of the home page. Right? Well, this solution works but it makes all the menu links on home page to be highlighted, because each of them “think” they are on the correct page. It does not look good.<\/p>\n My solution to the problem is to dynamically prefix an anchor link with “http:\/\/www.mywebsite.com” when it is not on the homepage. The piece of code below does exactly that.<\/p>\n We change nav_menu <\/strong>code by hooking into wp_nav_menu_objects<\/strong> filter. $sorted_menu_items <\/strong>and $args <\/strong>are passed from the filter. $args <\/strong>are arguments created by the user, but we not gonna touch them. We will work with $sorted_menu_items. <\/b>It is an array of objects. First we make sure, we are not<\/strong> on front page. Then we start looping through all the objects and checking if link starts with a ‘#’ sign. Actually, in the code above I am checking if the link does not start with ‘http’ or https’ because I felt like playing with regular expressions. For a simple check if a string starts with ‘#’ character substr<\/span>(<\/span> $menuLink<\/span>,<\/span> 0<\/span>,1<\/span> )<\/span> ===<\/span> “#”<\/span><\/strong> should do. If the link starts with ‘#’ character, we need to append website’s url<\/strong> to it. In the end, do not forget to return $sorted_menu_items<\/strong>. This is the difference of filter hook from action hook. In filter hook you are passed an argument. You do something to it and then return it.<\/p>\n <\/p>\n\n add_filter( 'wp_nav_menu_objects', 'em_links');\n\tfunction em_links ( $sorted_menu_items, $args ) {\n\t\tif(!is_front_page()){\t \n \t\tforeach ($sorted_menu_items as $menu_item) {\n\t\n\t\t\t$menuLink = $menu_item->url;\n\t\t\tif(!preg_match(\"~^(?:f|ht)tps?:\/\/~i\", $menuLink)){\n \t\t\t\t$menu_item->url = get_bloginfo('url') . $menuLink;\n\t\t\t}\n\n \t\t}\n \n \t\t}\n\treturn $sorted_menu_items;\n\t}\n<\/code>\n<\/pre>\n