April 29, 2024

Add Current Page Title to WordPress Menu with a Snippet

Play Video

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:


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

  1. Navigate to Snippets > Add New to create a new snippet. Name the snippet appropriately, such as “Menu Name”
  2. 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
    Copy the following code snippet and paste it into the code area

  3. Save the snippet and ensure it’s activated.
  4. Navigate to Appearance > Menus and select the menu where you want to add the current page title.
  5. Add a custom link by clicking Add menu items > Custom Links:
    1. Enter a hashtag (#) in the URL field, as this link won’t navigate anywhere.
    2. Add the shortcode [current_page_title] to the “Link Text” field.
      Add a custom link by clicking Add menu items Custom Links
  6. Click “Add to Menu” to insert the new item into your WordPress menu and rearrange the menu items as needed.
  7. Save the menu to apply the changes. Don’t forget to test your changes.
    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.

 

Required Resources

Code Snippets Logo
Free Options
Credit to Web Squadron
Web Squadron is a popular YouTube channel dedicated to providing informative tutorials, tips, and insights on web development and design. With a strong focus on WordPress and website builders like Bricks Builder, the channel aims to help users of all skill levels, from beginners to advanced developers, create and optimize their websites. By offering easy-to-understand, step-by-step guidance, Web Squadron provides practical and valuable content.
Visit
Add Current Page Title to WordPress Menu with a Snippet
Welcome back!
Enter your Helwp credentials to sign in.

No Account yet? Sign Up

My Account
Menu
Give Feedback
Describe your feedback *
Rate Helwp
Share
Facebook
Twitter
LinkedIn
Reddit
Email
WhatsApp
Telegram
Pocket
Report
Problem *
Describe the problem
Want us to reply?
Your E-Mail
Affiliate Disclosure

At Helwp, we’re committed to transparency and honesty. Therefore, we want to inform you that some of the links on our website are affiliate links. This means that, at no additional cost to you, we may earn a small commission if you click through and make a purchase.

We only promote products or services that we genuinely believe in. These affiliate commissions help us to maintain the website and continue to provide you with free, high-quality WordPress content.

If you are interested in how you can support us even further, check out our support page.