admin管理员组文章数量:1320173
I am creating a menu where pages are automatically assigned to it. However I need to check whether the page already exists in the menu before adding it.
Currently I am using the following code:
// Create header menu
$header_menu_name = 'Header Nav';
$header_menu_exists = wp_get_nav_menu_object( $header_menu_name );
$header_menu_id = null;
if ( $header_menu_exists ) {
$header_menu_id = $header_menu_exists->term_id;
} else {
$header_menu_id = wp_create_nav_menu($header_menu_name);
}
$pages = get_pages( array( 'sort_column' => 'menu_order' ) );
foreach ($pages as $page) {
// IF PAGE DOES NOT EXIST IN MENU...
wp_update_nav_menu_item( $header_menu_id, 0, array(
'menu-item-title' => $page->post_title,
'menu-item-url' => get_page_link( $page->ID ),
'menu-item-status' => 'publish'
));
}
$locations['header-nav'] = $header_menu_id;
// Set menu locations
set_theme_mod('nav_menu_locations', $locations);
I have commented out where I think the if statement should be placed.
Any ideas on how to approach this would be greatly appreciated!
本文标签: Check if Page Already Exists in Menu
版权声明:本文标题:Check if Page Already Exists in Menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742060509a2418555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论