Wordpress: создавать страницы через базу данных? - PullRequest
0 голосов
/ 22 декабря 2011

В настоящее время я работаю над новым туристическим сайтом, но у меня проблемы с одной вещью:

У меня есть список всех стран, регионов и городов, которые я хочу опубликовать. Как мне быстро создать страницу для всех них так:

  • Каждая страница должна быть подстраницей, например: страна / регион / город
  • Каждая страница должна иметь определенный шаблон страницы

Пожалуйста, дайте мне знать, заранее спасибо за ваше время и информацию!

1 Ответ

3 голосов
/ 22 декабря 2011

Вы можете сделать что-то вроде этого.

<?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;
    }

Предупреждение: Убедитесь, что выполняется только один раз, или добавьте проверку, чтобы избежать дублирования страниц.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...