2012-09-19 37 views
0

我創建了一個插件,通過使用此代碼創作的WordPress插件創建子菜單的

add_filter('page_template', 'in_page_template'); 
function in_page_template() 
{ 
    global $wpdb; 
    $new_page_title = 'Packages'; 
    $sql = "SELECT * FROM wp_posts where post_name='" . $new_page_title . "';"; 
    $cnt_post = $wpdb->get_results($sql); 
    if (!(is_page('Home'))) { 
     $ppid = $_GET['page_id']; 
     if (count($cnt_post) != 0) { 
      $pid = $cnt_post[0]->ID; 
      if ($pid == $ppid) { 
       $page_template = dirname(__FILE__) . '/Packages.php'; 
       return $page_template; 
      } 
     } 
    } 
} 

我怎樣才能爲包頁面創建子菜單創建菜單

+1

你不應該使用的表你的查詢中的名字用'$ wpdb-> posts'替換'wp_posts' http://codex.wordpress.org/wpdb#Tables – janw

回答

0
add_action('init', 'create_initial_pages'); 
function create_initial_pages() { 
$pages = array(
    array(
     'name' => 'post_name', 
     'title' => 'post_title', 
     'child' => array(
      'page1-1' => 'National', 
      'page1-2' => 'International' 

     ) 
    ), 

); 

$template = array(
    'post_type' => 'page', 
    'post_status' => 'publish', 
    'post_author' => 1 
); 

foreach($pages as $page) { 
    $exists = get_page_by_title($page['title']); 
    $my_page = array(
     'post_name' => $page['name'], 
     'post_title' => $page['title'] 
    ); 
    $my_page = array_merge($my_page, $template); 

    $id = ($exists ? $exists->ID : wp_insert_post($my_page)); 

    if(isset($page['child'])) { 
     foreach($page['child'] as $key => $value) { 
      $child_id = get_page_by_title($value); 
      $child_page = array(
       'post_name' => $key, 
       'post_title' => $value, 
       'post_parent' => $id 
      ); 
      $child_page = array_merge($child_page, $template); 
      if(!isset($child_id)) wp_insert_post($child_page); 
     } 
    } 
    } 
}