Creating a dynamic menu in Vue.js within a WordPress environment involves several steps, including setting up a WordPress API endpoint to fetch menu data and using Vue.js to render the menu dynamically on the frontend. Here’s how you can do it:

1. Create a WordPress REST API Endpoint for the Menu

First, you’ll need to create a custom REST API endpoint in WordPress that returns the menu data. You can do this by adding the following code to your theme’s functions.php file or creating a custom plugin:

function get_custom_menu() {
    $menu_name = 'your-menu-slug'; // Replace with your menu slug
    $locations = get_nav_menu_locations();
    $menu_id = $locations[$menu_name];
    $menu_items = wp_get_nav_menu_items($menu_id);

    $formatted_menu = [];

    foreach ($menu_items as $menu_item) {
        $formatted_menu[] = [
            'title' => $menu_item->title,
            'url' => $menu_item->url,
            'children' => [],
        ];
    }

    return $formatted_menu;
}

add_action('rest_api_init', function() {
    register_rest_route('custom/v1', '/menu', array(
        'methods' => 'GET',
        'callback' => 'get_custom_menu',
    ));
});

This code creates a REST API endpoint at /wp-json/custom/v1/menu that returns the menu items in a JSON format.

2. Fetch the Menu in Vue.js

In your Vue.js component, you can fetch this menu data and render it dynamically. Here’s an example of how you might do that:

<template>
  <div>
    <ul>
      <li v-for="item in menuItems" :key="item.url">
        <a :href="item.url">{{ item.title }}</a>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [],
    };
  },
  mounted() {
    this.fetchMenu();
  },
  methods: {
    async fetchMenu() {
      try {
        const response = await fetch('https://yourwordpresssite.com/wp-json/custom/v1/menu');
        const data = await response.json();
        this.menuItems = data;
      } catch (error) {
        console.error('Error fetching menu:', error);
      }
    },
  },
};
</script>

Replace https://yourwordpresssite.com with your actual WordPress site URL.

3. Render the Menu in Vue.js

The template section in the Vue component renders the menu items dynamically. You can customize this to include submenus or other features as needed.

4. Include Vue.js in Your WordPress Theme

Make sure that Vue.js is properly integrated into your WordPress theme. You can do this by enqueuing Vue.js in your theme’s functions.php file:

function enqueue_vue_scripts() {
    wp_enqueue_script('vue-js', 'https://cdn.jsdelivr.net/npm/vue@2', [], null, true);
    wp_enqueue_script('your-custom-js', get_template_directory_uri() . '/js/custom-vue.js', ['vue-js'], null, true);
}

add_action('wp_enqueue_scripts', 'enqueue_vue_scripts');

5. Deploy and Test

After setting everything up, deploy your changes to your WordPress site and test the dynamic menu to ensure it loads correctly and updates in response to changes in the WordPress menu.

Notes:

  • If your menu has submenus, you may need to modify the WordPress API code to include child items and update your Vue.js code to handle nested lists.
  • Ensure your WordPress site supports REST API and that any required plugins or settings are configured to allow the endpoint to be accessed correctly.

By following these steps, you should be able to create a dynamic menu in Vue.js that is powered by WordPress and updates based on the WordPress menu configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending