wordpress 中 菜单是如何存储在数据库中的

一直没有深入的了解wordpress中菜单是怎么实现的,在wordpress 中菜单很灵活,也很强大。

添加一个菜单,看看数据表中增加了什么信息

wordpress 中创建一个菜单

数据表中 wp_terms

select * from wp_terms;
+---------+-----------------+-----------------------------------------------+------------+
| term_id | name            | slug                                          | term_group |
+---------+-----------------+-----------------------------------------------+------------+
|      24 | 第一个菜单      | %e7%ac%ac%e4%b8%80%e4%b8%aa%e8%8f%9c%e5%8d%95 |          0 |
+---------+-----------------+-----------------------------------------------+------------+

数据表 wp_term_taxonomy

 select * from wp_term_taxonomy ;
+------------------+---------+----------+-------------+--------+-------+
| term_taxonomy_id | term_id | taxonomy | description | parent | count |
+------------------+---------+----------+-------------+--------+-------+
|               24 |      24 | nav_menu |             |      0 |     0 |
+------------------+---------+----------+-------------+--------+-------+

添加两个菜单项

  1. 一个文章链接
  2. 一个自定义链接
添加菜单的项目

看看 wp_posts 表

select ID, post_title, post_status, post_name, post_type from wp_posts  order by id  desc  limit 2;
+----+------------+-------------+-----------+---------------+
| ID | post_title | post_status | post_name | post_type     |
+----+------------+-------------+-----------+---------------+
| 96 | wpcode     | publish     | wpcode    | nav_menu_item |
| 95 |            | publish     | 95        | nav_menu_item |
+----+------------+-------------+-----------+---------------+

看看 wp_postmeta 表

select * from wp_postmeta  where post_id in (95, 96) order by meta_id asc  ;
+---------+---------+-----------------------------+-----------------------+
| meta_id | post_id | meta_key                    | meta_value            |
+---------+---------+-----------------------------+-----------------------+
|     236 |      95 | _menu_item_type             | post_type             |
|     237 |      95 | _menu_item_menu_item_parent | 0                     |
|     238 |      95 | _menu_item_object_id        | 1                     |
|     239 |      95 | _menu_item_object           | post                  |
|     240 |      95 | _menu_item_target           |                       |
|     241 |      95 | _menu_item_classes          | a:1:{i:0;s:0:"";}     |
|     242 |      95 | _menu_item_xfn              |                       |
|     243 |      95 | _menu_item_url              |                       |
|     245 |      96 | _menu_item_type             | custom                |
|     246 |      96 | _menu_item_menu_item_parent | 0                     |
|     247 |      96 | _menu_item_object_id        | 96                    |
|     248 |      96 | _menu_item_object           | custom                |
|     249 |      96 | _menu_item_target           |                       |
|     250 |      96 | _menu_item_classes          | a:1:{i:0;s:0:"";}     |
|     251 |      96 | _menu_item_xfn              |                       |
|     252 |      96 | _menu_item_url              | https://www.wpcode.cn |
+---------+---------+-----------------------------+-----------------------+

可以看到添加一个菜单 ,回字 分类表中 wp_terms 增加一项,同时在 分类方法表 wp_term_taxonomy 中也增加一项。所以在wordpress 中一个菜单本质就是一个分类。菜单的内容,就是分类的项。