WordPress切换主题后自动生成页面

Published
2023-03-09
浏览次数 :  114

function add_custom_pages() {
    // Create an array of pages to add
    $pages = array(
        array(
            'title' => 'Sample Page 1',
            'slug' => 'sample-page-1',
            'content' => 'This is a sample page created programmatically.',
            'template' => 'page-custom.php'
        ),
        array(
            'title' => 'Sample Page 2',
            'slug' => 'sample-page-2',
            'content' => 'This is another sample page created programmatically.',
            'template' => 'page-custom.php'
        )
    );
    
    // Loop through the array and create the pages
    foreach ($pages as $page) {
        $page_check = get_page_by_title($page['title']);
        $new_page = array(
            'post_type' => 'page',
            'post_title' => $page['title'],
            'post_content' => $page['content'],
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => $page['slug']
        );
        
        if(!isset($page_check->ID)){
            $new_page_id = wp_insert_post($new_page);
            if(!empty($page['template'])){
                update_post_meta($new_page_id, '_wp_page_template', $page['template']);
            }
        }
    }
}
add_action('after_switch_theme', 'add_custom_pages');

  • 标签1
  • 标签1
  • 标签1
Top