> For the complete documentation index, see [llms.txt](https://jks.gitbook.io/tai-lieu-wordpress/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jks.gitbook.io/tai-lieu-wordpress/chuong-4-lap-trinh-theme/lap-trinh-theme-wordpress/tao-menu-va-get-menu-trong-wordpress.md).

# Tạo menu và get menu trong wordpress

### Tạo menu trong wordpress <a href="#tao-menu-trong-wordpress-0" id="tao-menu-trong-wordpress-0"></a>

Menu trong wordpress nằm ở: **Admin -> giao diện -> menu**

Nhưng khi mới tạo theme thì khu vực menu này sẽ không xuất hiện, tại vì để website hỗ trợ chức năng menu thì anh em phải khai báo các vị trí hiển thị menu trước.

![Hướng dẫn tạo menu và get menu trong wordpress](https://hocwordpress.vn/wp-content/uploads/2020/05/lay-menu-trong-wordpress.png)

*Khi mới tạo theme website sẽ không hỗ trợ chức năng menu*

#### Khai báo vị trí hiển thị của menu <a href="#khai-bao-vi-tri-hien-thi-cua-menu-1" id="khai-bao-vi-tri-hien-thi-cua-menu-1"></a>

Để khai báo các vị trí hiển thị của menu các bạn chèn đoạn code sau vào file **functions.php** của theme đang sử dụng nhé.

```php
function register_my_menu() {
    register_nav_menu('header-menu',__( 'Menu chính' ));
    register_nav_menu('footer-menu',__( 'Menu Footer' ));
}
add_action( 'init', 'register_my_menu' );p
```

Đoạn code trên mình khai báo 2 menu.

* Menu header hiển thi menu phần trên của website có id là: **header-menu**
* Menu footer hiển thi menu dưới footer của website có id là: **footer-menu**

**Chú ý:** Các **id** này mình sẽ dùng để get menu nhé.

#### Thêm nội dung vào menu <a href="#them-noi-dung-vao-menu-2" id="them-noi-dung-vao-menu-2"></a>

Sau khi khai báo vị trí hiển thị menu trong wordpress các bạn vào phần giao diện sẽ thấy xuất hiện như hình dưới

![Hướng dẫn tạo menu và get menu trong wordpress](https://hocwordpress.vn/wp-content/uploads/2020/05/tao-menu.jpg)

Việc bay chừ là thêm các nội dung vào 2 vị trí menu đã tạo. Các bạn xem bài viết [tạo menu trong wordpress](https://hocwordpress.vn/tao-menu-trong-wordpress) để biết cách làm nha.

### Get menu wordpress <a href="#get-menu-wordpress-3" id="get-menu-wordpress-3"></a>

Để get meu ra ngoài front end chúng ta sử dụng hàm **wp\_nav\_menu.** Cụ thể chúng ta chèn đoạn code sau vào vị trí cần hiển thị nha.

#### Code hiển thị menu header <a href="#code-hien-thi-menu-header-4" id="code-hien-thi-menu-header-4"></a>

```php
<?php wp_nav_menu( 
  array( 
      'theme_location' => 'header-menu', 
      'container' => 'false', 
      'menu_id' => 'header-menu', 
      'menu_class' => 'menu'
   ) 
); ?>
```

#### Code hiển thị menu footer <a href="#code-hien-thi-menu-footer-5" id="code-hien-thi-menu-footer-5"></a>

```php
<?php wp_nav_menu( 
  array( 
      'theme_location' => 'footer-menu', 
      'container' => 'false', 
      'menu_id' => 'footer-menu', 
      'menu_class' => 'menu'
   ) 
); ?>
```

#### **Giải thích:**

* **wp\_nav\_menu** là hàm get menu.
* **‘theme\_location’** là id menu mà bạn muốn lấy, ở trường họp này mình lấy id mình vừa khởi tạo ở trên
* **‘container’** là thẻ div bao bọc bên ngoài menu, (có hoặc không).
* **‘menu\_id’** là id của thẻ ul khi hiển thị menu.
* **‘menu\_class’** là class của thẻ ul khi hiển thị menu.
