2017-10-08 52 views
0

我有下面的代碼在激活我的wordpress插件時執行。WP_insert_post也添加了一個菜單項

它確實創建了頁面,但它也添加了一個我不想擁有的導航菜單項。

有人可以幫助我,因爲我無法看到我出錯的地方。

我知道它還添加了一個導航菜單項,因爲查看錶wp_posts中的數據庫,post_date對於post_type Page和nav_menu_item完全相同。

public static function activate() { 
    global $myplugin; 
    require_once plugin_dir_path(__FILE__) . 'install-sql.php'; 

    //Add a front end page 
    $author_id = 9; 
    $slug = 'myplugin'; 
    $title = "My Plugin"; 
    $content = '[myplugin_render]'; 
    $page = array(
     'post_author'   => 1, 
     'post_content'   => $content, 
     'post_title'   => $title, 
     'post_status'   => 'publish', 
     'post_type'    => 'page', 
     'comment_status'  => 'closed', 
     'ping_status'   => 'closed', 
     'guid'     => '', 
     'import_id'    => 0, 
     'context'    => '' 
    ); 
    if(null == get_page_by_title($title)) { 
     wp_insert_post($page); 
    } else { 
     $page = get_page_by_title($title); 
     if (is_page($page->ID)) { 
      $post = array(
       'ID'   => $page->ID, 
       'post_content' => $content 
       ); 
      wp_update_post($post); 
     } 
    } 



} 

回答

1

胡亂猜測:進入外觀=>菜單,選擇你的菜單的設置下,是旁邊的複選框「自動添加新的頂級頁面,這個菜單中的」檢查?

如果是:取消選中:)

+0

你我的朋友,是天才!非常感謝,這讓我瘋狂! – Caveman