最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Check if Page Already Exists in Menu

programmeradmin1浏览0评论

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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论