Have a look at how to add the current page title to WordPress menu, which can be helpful for location-based pages or unique services. This tutorial demonstrates how to add the current page title to a WordPress menu using a code snippet. This method is straightforward and requires only a snippet plugin.
Prerequisites
Before you begin, ensure you have the following installed and activated:
- Code Snippets (or similar snippet plugins)
Use Cases for Adding Current Page Title to WordPress Menu
This addition can be useful in several scenarios where clear and dynamic navigation is needed. Here are some use cases:
- Location-based Websites
If your website represents multiple locations (e.g., different branches or offices), displaying the current location in the menu helps users identify where they are. - Service-based Websites
When offering a variety of services, displaying the current page title in the menu makes it easier for visitors to understand which service they are viewing. - Dynamic Content
For websites with frequently updated content or dynamically generated pages, displaying the page title in the menu can provide a clear context for visitors. - Portfolio or Project Websites
If your website showcases multiple projects or portfolio items, this feature allows visitors to see which project or item they are currently exploring. - E-commerce Platforms
In online stores, this addition can be helpful when navigating between different product categories, allowing customers to quickly understand where they are in the store.
Instructions to Add Current Page Title to WordPress Menu
- Navigate to Snippets > Add New to create a new snippet. Name the snippet appropriately, such as “Menu Name”
- Copy the following code snippet and paste it into the code area:
function custom_page_title_shortcode() { return get_the_title(); } add_shortcode('current_page_title', 'custom_page_title_shortcode'); function execute_shortcodes_in_menus($menu_item) { if (strpos($menu_item->title, '[') !== false && strpos($menu_item->title, ']') !== false) { $menu_item->title = do_shortcode($menu_item->title); } return $menu_item; } add_filter('wp_nav_menu_objects', 'execute_shortcodes_in_menus');
Source: Code Snippets Cloud
- Save the snippet and ensure it’s activated.
- Navigate to Appearance > Menus and select the menu where you want to add the current page title.
- Add a custom link by clicking Add menu items > Custom Links:
- Click “Add to Menu” to insert the new item into your WordPress menu and rearrange the menu items as needed.
- Save the menu to apply the changes. Don’t forget to test your changes.
Official Documentation
Here are the links to the official documentation for resources used in this tutorial:
Current Page Title to WordPress Menu – Conclusion
Adding the current page title to your WordPress menu can enhance navigation and improve the user experience, especially on location-based websites. With this simple code snippet, you can customize your WordPress menu to dynamically display the title of the current page, providing a more informative and interactive experience for your visitors.