wordpress-菜单相关函数

♦  菜单数据获取

◆ wp_get_nav_menu_object(‘菜单3’)

作用:根据菜单 ID 或菜单名称获取菜单对象

◆ get_nav_menu_locations()

作用:获取这些已注册菜单位置和分配给它们的菜单 ID 之间的映射关系。

◆ wp_get_nav_menu_items($footer_menu_id2)

作用:获取指定菜单的所有菜单项,返回一个包含菜单项对象的数组

<?php
$locations = get_nav_menu_locations();
$footer_menu_id = $locations['footer-menu'];
//获取底部菜单所有项目
$footer_menus = wp_get_nav_menu_items($footer_menu_id);
$footer_menu_id2 = $locations['footer-menu2'];
$footer_menus2 = wp_get_nav_menu_items($footer_menu_id2);
echo '<pre>';
print_r(wp_get_nav_menu_object('菜单3'));
wp_die();
?>

<ul class="footer-menu">
    <?php
    foreach ($footer_menus2 as $foot_menu){
        ?>
        <li>
            <a href="<?php echo esc_url($foot_menu->url); ?>" >
                <?php echo esc_html($foot_menu ->title); ?>
                <?php echo '&nbsp;' ?>
            </a>
        </li>
        <?php
    }
    ?>
</ul>

测试输出如下:

//print_r(wp_get_nav_menu_object('菜单3'));
WP_Term Object
(
    [term_id] => 5
    [name] => 菜单3
    [slug] => %e8%8f%9c%e5%8d%953
    [term_group] => 0
    [term_taxonomy_id] => 5
    [taxonomy] => nav_menu
    [description] => 
    [parent] => 0
    [count] => 4
    [filter] => raw
)

//print_r($locations);
Array
(
    [primary-menu] => 3
    [footer-menu] => 4
    [footer-menu2] => 5
)

//$footer_menus2
Array
(
    [0] => WP_Post Object
        (
            [ID] => 15
            [post_author] => 1
            [post_date] => 2025-02-08 11:38:35
            [post_date_gmt] => 2025-02-08 03:38:35
            [post_content] => 
            [post_title] => © 2024 行深技术工作室
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 2024-%e8%a1%8c%e6%b7%b1%e6%8a%80%e6%9c%af%e5%b7%a5%e4%bd%9c%e5%ae%a4
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2025-02-08 11:38:35
            [post_modified_gmt] => 2025-02-08 03:38:35
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://example3.chenzhuzhen.cn/?p=15
            [menu_order] => 1
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 15
            [menu_item_parent] => 0
            [object_id] => 15
            [object] => custom
            [type] => custom
            [type_label] => 自定义链接
            [title] => © 2024 行深技术工作室
            [url] => https://www.chenzhuzhen.cn/
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [1] => WP_Post Object
        (
            [ID] => 16
            [post_author] => 1
            [post_date] => 2025-02-08 11:38:35
            [post_date_gmt] => 2025-02-08 03:38:35
            [post_content] => 
            [post_title] => 使用条款
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => %e4%bd%bf%e7%94%a8%e6%9d%a1%e6%ac%be
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2025-02-08 11:38:35
            [post_modified_gmt] => 2025-02-08 03:38:35
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://example3.chenzhuzhen.cn/?p=16
            [menu_order] => 2
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 16
            [menu_item_parent] => 0
            [object_id] => 16
            [object] => custom
            [type] => custom
            [type_label] => 自定义链接
            [title] => 使用条款
            [url] => #
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [2] => WP_Post Object
        (
            [ID] => 17
            [post_author] => 1
            [post_date] => 2025-02-08 11:38:35
            [post_date_gmt] => 2025-02-08 03:38:35
            [post_content] => 
            [post_title] => 隐私政策
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => %e9%9a%90%e7%a7%81%e6%94%bf%e7%ad%96
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2025-02-08 11:38:35
            [post_modified_gmt] => 2025-02-08 03:38:35
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://example3.chenzhuzhen.cn/?p=17
            [menu_order] => 3
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 17
            [menu_item_parent] => 0
            [object_id] => 17
            [object] => custom
            [type] => custom
            [type_label] => 自定义链接
            [title] => 隐私政策
            [url] => #
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [3] => WP_Post Object
        (
            [ID] => 18
            [post_author] => 1
            [post_date] => 2025-02-08 11:38:35
            [post_date_gmt] => 2025-02-08 03:38:35
            [post_content] => 
            [post_title] => 闽ICP备2025083626号-1
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => %e9%97%bdicp%e5%a4%872025083626%e5%8f%b7-1
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2025-02-08 11:38:35
            [post_modified_gmt] => 2025-02-08 03:38:35
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://example3.chenzhuzhen.cn/?p=18
            [menu_order] => 4
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 18
            [menu_item_parent] => 0
            [object_id] => 18
            [object] => custom
            [type] => custom
            [type_label] => 自定义链接
            [title] => 闽ICP备2025083626号-1
            [url] => http://beian.miit.gov.cn/
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

)

注册菜单地址:

function example3_setup() {
    register_nav_menus(
        array(
            'primary-menu' => __( 'Primary Menu', 'example3' ),
            'footer-menu' => __( 'Footer Menu', 'example3' ),
            'footer-menu2' => __( 'Footer Menu2', 'example3' )
        )
    );
}
add_action( 'after_setup_theme', 'example3_setup' );

◆ 菜单相关sql表

相关sql如下:

select * from xs_3_options;

wordpress后台菜单管理页面配置如下:

select * from xs_3_terms;
select id,post_title from xs_3_posts where post_type='nav_menu_item';
select id,post_title from xs_3_posts where post_type='nav_menu_item';
select * from xs_3_term_taxonomy where taxonomy='nav_menu';
select * from xs_3_postmeta ;

♦  标题2

◆ 标题3

段落

段落

段落