You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅克隆WordPress站点URL至新站点?需空白内容无媒体文件

克隆WordPress站点URL结构(空内容、无媒体)的解决方案

以下是几种无需迁移插件的实现方法,完全满足只复制页面URL结构、内容为空且不带媒体的需求:

方法一:使用WP CLI(最便捷高效)

WP CLI是WordPress官方命令行工具,适合熟悉命令行的用户:

  1. 导出原站点页面结构元数据
    在原站点服务器执行命令,导出页面的标题、slug、状态、父页面ID等关键信息(自动排除内容和媒体):
    wp post list --post_type=page --fields=post_title,post_name,post_status,post_parent --format=json > site-structure.json
    
  2. 在新站点创建空页面
    将导出的site-structure.json上传到新站点服务器,执行以下bash脚本循环创建页面(需提前安装jq工具解析JSON):
    jq -c '.[]' site-structure.json | while read -r post; do
        title=$(echo "$post" | jq -r '.post_title')
        slug=$(echo "$post" | jq -r '.post_name')
        status=$(echo "$post" | jq -r '.post_status')
        parent=$(echo "$post" | jq -r '.post_parent')
        wp post create --post_type=page --post_title="$title" --post_name="$slug" --post_status="$status" --post_parent="$parent" --post_content=""
    done
    
    执行后会生成所有对应URL的空页面,完全保留原站点的层级结构和发布状态。

方法二:数据库SQL操作(适合有数据库权限的用户)

直接操作数据库可精准控制复制的字段:

  1. 导出原站点页面结构
    在原站点数据库执行SQL语句,导出页面核心结构字段(过滤内容、附件相关数据):
    SELECT post_title, post_name, post_status, post_parent, post_type, post_date, post_date_gmt
    FROM wp_posts
    WHERE post_type = 'page' AND post_status IN ('publish', 'draft', 'private')
    INTO OUTFILE '/tmp/site-pages-structure.csv'
    FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    LINES TERMINATED BY '\n';
    
    (注意替换wp_为你的数据库表前缀)
  2. 导入到新站点数据库
    将导出的CSV文件导入新站点数据库,执行插入语句时强制设置post_content为空:
    INSERT INTO wp_posts (post_title, post_name, post_status, post_parent, post_type, post_date, post_date_gmt, post_content)
    VALUES 
    ("首页", "home", "publish", 0, "page", "2024-01-01 00:00:00", "2024-01-01 00:00:00", ""),
    ("关于我们", "about-us", "publish", 0, "page", "2024-01-01 00:00:00", "2024-01-01 00:00:00", "");
    
    执行前务必备份新站点数据库,避免数据冲突。

方法三:自定义PHP脚本(适合开发者)

通过简单脚本实现结构导出和导入:

  1. 原站点导出脚本
    在原站点根目录创建export-structure.php,访问后生成结构文件:
    <?php
    require_once('wp-load.php');
    
    $pages = get_pages([
        'post_status' => ['publish', 'draft', 'private'],
        'numberposts' => -1
    ]);
    
    $structure = [];
    foreach ($pages as $page) {
        $structure[] = [
            'title' => $page->post_title,
            'slug' => $page->post_name,
            'status' => $page->post_status,
            'parent' => $page->post_parent
        ];
    }
    
    file_put_contents('site-structure.json', json_encode($structure));
    echo '结构导出完成,已生成site-structure.json';
    ?>
    
    下载生成的site-structure.json文件。
  2. 新站点导入脚本
    将site-structure.json上传到新站点根目录,创建import-structure.php:
    <?php
    require_once('wp-load.php');
    
    $structure = json_decode(file_get_contents('site-structure.json'), true);
    
    foreach ($structure as $page_data) {
        wp_insert_post([
            'post_title' => $page_data['title'],
            'post_name' => $page_data['slug'],
            'post_status' => $page_data['status'],
            'post_parent' => $page_data['parent'],
            'post_type' => 'page',
            'post_content' => '',
            'post_excerpt' => ''
        ]);
    }
    
    echo '页面结构导入完成';
    ?>
    
    访问该脚本完成导入,之后立即删除两个脚本保障站点安全。

内容的提问来源于stack exchange,提问作者ahmed ghaly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 06:05:50