【问题标题】:Exporting Ghost to Wordpress将 Ghost 导出到 Wordpress
【发布时间】:2021-05-30 09:51:37
【问题描述】:

我一直在寻找将我的 Ghost 博客文章复制到 Wordpress 的可能性。

到目前为止,我已经成功地将所有幽灵数据导出到一个 JSON 文件——你知道任何现有的工具可以将它转换为 Wordpress 可以导入的东西吗?

如果不是,我必须自己构建一些东西,您是否建议将 JSON 解析为 WXR 文件或类似文件,或者直接导入 Wordpress 的数据库?

提前致谢! K.

【问题讨论】:

    标签: json wordpress parsing


    【解决方案1】:

    我将 Ghost 博客迁移到 Wordpress,方法是阅读 Ghost JSON 导出文件,稍微按摩一下,然后使用 wp_insert_post 导入帖子。

    此代码应放在主题的 functions.php 文件中 - 您可以在 http://example.com/ghost/debug/ 导出您的 Ghost 帖子 (GhostData.json)。

    注意:下面的示例不会导入所有数据,而是导入大部分关键字段。

    /**
     *  A function used to programmatically create a post in WordPress.
     *
     *  http://tommcfarlin.com/programmatically-create-a-post-in-wordpress/
     *
     *  @returns post ID if successful
     *           -1 if the post was never created
     *           -2 if a post with the same title exists
     */
    function create_wp_post ($post_details) {
        // Initialize the page ID to -1. This indicates no action has been taken.
        $post_id = -1;
    
        $post = get_page_by_title($post_details['title'], 'OBJECT', 'post');
    
        // If the page title doesn't already exist, then insert the post
        if (is_null($post)) {
            // Set the post ID so that we know the post was created successfully
            $post_id = wp_insert_post(
                array(
                    'comment_status'    =>  'closed',
                    'ping_status'       =>  'closed',
                    'post_author'       =>  $post_details['author'],
                    'post_content'      =>  $post_details['content'],
                    'post_date'         =>  $post_details['date'],
                    'post_date_gmt'     =>  $post_details['date_gmt'],
                    'post_name'         =>  $post_details['slug'],
                    'post_status'       =>  $post_details['status'],
                    'post_title'        =>  $post_details['title'],
                    'post_type'         =>  'post',
                    'tags_input'        =>  $post_details['tags_input']
                )
            );
        // Page title already exists, return error
        } else {
            $post_id = -2;
        }
    }
    
    /**
     *  A function used to filter Ghost blog posts into Wordpress format.
     */
    function filter_ghost_posts () {
        $posts = json_decode(file_get_contents('GhostData.json'), true);
    
        if ($posts) { 
            foreach ($posts['data']['posts'] as $post) {
                $post_details = array(
                    'author'        => $post['author_id'],
                    'date'          => date('Y-m-d H:i:s', $post['published_at'] / 1000),
                    'date_gmt'      => gmdate('Y-m-d H:i:s', $post['published_at'] / 1000),
                    'id'            => $post['id'],
                    'content'       => $post['html'],
                    'status'        => $post['status'],
                    'slug'          => $post['slug'],
                    'title'         => $post['title']
                );
    
                // Status
                // Fix discrepancy in naming between Ghost and Wordpress
                if ($post_details['status'] === 'published') {
                    $post_details['status'] = 'publish';
                }
    
                // Tags
                $post_tags_list = [];
    
                foreach ($posts['data']['posts_tags'] as $post_tags) {
                    if ($post['id'] === $post_tags['post_id']) {
                        $post_tags_id = $post_tags['tag_id'];
                        array_push($post_tags_list, $posts['data']['tags'][$post_tags_id]['name']);
                    }
                }
    
                if (count($post_tags_list) > 0) {
                    $post_details['tags_input'] = implode(',', $post_tags_list);
                }
    
                $post_id = create_wp_post($post_details);
    
                if ($post_id == -1 || $post_id == -2) {
                    // Error handling here
                }
            }
        } 
    }
    
    add_filter('after_setup_theme', 'filter_ghost_posts');
    

    【讨论】:

    • 可能的澄清:何时运行或如何启动它?导入完成后是否应该将其删除? json文件应该和functions.php文件放在同一个文件夹吗?
    • after_setup_theme 钩子在页面加载时被调用(我确信有一个更聪明的方法来启动它)。它会检查 Wordpress 中是否已经存在帖子标题(以防止重复),但无论如何我都建议在导入完成后将其删除。请注意,如果您的博客有重复的帖子标题,则需要删除该检查(但请注意仅加载一次!)。是的,JSON 文件应该与 functions.php 位于同一文件夹中(或相应地更新路径)。
    【解决方案2】:

    我的建议是使用 Google Refine 导入 JSON,导出 CSV,然后使用 WP Ultimate CSV Importer Plugin 将其导入您的 WordPress 网站。希望这可以帮助。

    【讨论】:

      【解决方案3】:

      这个问题已经很老了,但截至 2017 年,我仍然无法找到 Ghost > WP 帖子迁移的解决方案。我所做的是:

      1. 导出 JSON 数据,如此处所述 - https://help.ghost.org/hc/en-us/articles/224112927-Import-Export-Data
      2. 将一些 JSON 字段,如 title、markdown... 更改为 post_title 和 post_content 等(WP 帖子字段列表在这里 - https://codex.wordpress.org/Class_Reference/WP_Post),并删除一些不需要的字段,如 update_at 或 amp。我留下的是:

        身份证

        post_title

        post_name

        post_content

        post_status

        元标题

        元描述

        post_author

        发布日期

      3. 删除其他 JSON 字段/结构,因此只有“帖子”:[]

      4. 使用 JSON 到 CSV 转换器,比如这个 - https://konklone.io/json/ 并下载 CSV 结果文件。

      5. 现在,当您有 CSV 文件时,安装 WordPress CSV Importer 插件 - https://wordpress.org/plugins/wp-ultimate-csv-importer/(或类似)
      6. 将 CSV 文件上传到导入器,检查字段选择是否正确。
      7. 在您的 WP 仪表板上享受导入的帖子;)

      注意:

      1. Ghost 不支持将图像与其他数据一起导出(截至 2017 年 1 月 31 日)。

      2. 您可能希望将“post_status”从“发布”更改为“待处理”,因此在正确编辑之前不会立即发布帖子;)

      【讨论】:

        【解决方案4】:

        如果有人在将来寻找这个:

        您可以使用ghost-to-wp npm 包。这会将您从 Ghost 导出的 JSON 文件转换为可直接导入 WordPress 的 WordPress 就绪的 WXR 文件:

        $ npm i -g ghost-to-wp
        
        $ ghost-to-wp yourghostexport.json
        

        正如@lomza 所指出的,仍然无法将图像很好地导出为 Ghost 文件的一部分,因此您必须手动迁移图像。

        披露:我是ghost-to-wp的作者。

        【讨论】:

          【解决方案5】:

          我尝试了各种导入器,最终最好的解决方案是直接从 RSS URL 导入,例如 https://myghostblog.com/rss/import-xml-feed 插件。

          附:记住分页。默认情况下,您的 RSS 链接可能仅显示帖子的第一页,因此要导入其他页面,您必须手动将 /2/ /3/ 添加到 URL 并分别导入。 (至少,这是我需要做的。)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-12-10
            • 2012-04-23
            • 1970-01-01
            • 2012-12-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多