2011-12-22 4 views
0

現在、私は新しい旅行のウェブサイトに取り組んでいますが、1つの問題に問題があります:Wordpress:データベースからページを作成しますか?

私はすべての国、地域、都市のリストを公開したいと思います。

  • すべてのページには、特定のページテンプレート
  • を持っている必要があり、国/地域/都市:すべてのページのようなサブページである必要があり

    • :どのように私はすぐにこのようなそれらのすべてのページを作成しますあなたの時間と情報を事前に感謝してください!

    +0

    WPデータベースのコーデックスエントリをチェックアウトし、1ページを作成してどのデータが挿入されているかを確認することをお勧めします。それでは、あなたの国、地域、都市ごとにこのデータを複製するだけです。http://codex.wordpress.org/Database_Description – Pat

    答えて

    3

    このようなことができます。

    <?php 
        // $country_list = get_country_list(); // returns list, of the format eg. array('India' => 'Content for the country India', 'Australia' => 'Content for the country Australia') 
        // $region_list = get_region_list($country); // Get the list of regions for given country, Assuming similar format as country. 
        // $city_list = get_city_list($region); // Get the list of cities for given region, Assuming similar format as country 
    
        /* Code starts here...*/ 
        $country_list = get_country_list(); 
        foreach($country_list as $country_title => $country_content) { 
         $country_template = 'template_country.php'; 
         $country_page_id = add_new_page($country_title, $country_content, $country_template); 
         // validate if id is not 0 and break loop or take needed action. 
    
         $region_list = get_region_list($country_title); 
         foreach($region_list as $region_title => $region_content) { 
          $region_template = 'template_region.php'; 
          $region_page_id = add_new_page($region_title, $region_content, $region_template, $country_page_id); 
          // validate if id is not 0 and break loop or take needed action. 
    
          $city_list = get_city_list($region_title);          
          foreach($city_list as $city_title => $city_content) { 
           $city_template = 'template_city.php'; 
           add_new_page($city_title, $city_content, $city_template, $region_page_id); 
          }                
         }                 
        }                  
    
        function add_new_page($title, $content, $template_file, $post_parent = 0) { 
         $post = array();             
         $post['post_title'] = $title;          
         $post['post_content'] = $content;         
         $post['post_parent'] = $post_parent;        
         $post['post_status'] = 'publish'; // Can be 'draft'/'private'/'pending'/'future' 
         $post['post_author'] = 1; // This should be the id of the author. 
         $post['post_type'] = 'page'; 
         $post_id = wp_insert_post($post); 
    
         // check if wp_insert_post is successful 
         if(0 != $post_id) {  
          // Set the page template 
          update_post_meta($post_id, '_wp_page_template', $template_file); // Change the default template to custom template 
         }             
         return $post_id; 
        } 
    

    警告: 1回だけ実行されていることを確認したり、重複するページを避けるために、任意の検証を追加。

    関連する問題