json - 将 Ghost 导出到 Wordpress

标签 json wordpress parsing

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

到目前为止,我已经设法将所有幻影数据导出到一个 JSON 文件中——您知道任何现有工具可以将其转换为 Wordpress 可以导入的内容吗?

如果没有,并且我必须自己构建一些东西,您会建议将 JSON 解析为 WXR 文件或类似文件,还是直接导入到 Wordpress 的数据库中?

提前致谢! K.

最佳答案

我通过读取 Ghost JSON 导出、稍微修改它并使用 wp_insert_post 导入帖子,将 Ghost 博客迁移到 Wordpress。

此代码应放在主题的 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 - 将 Ghost 导出到 Wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634246/

相关文章:

c++ - 如何将 json 数据从 Poco c++ json 转换为 XML?

wordpress - 我们可以将 nginx 同时用于 WordPress 和 Drupal 吗?

解析GF(2)中的代数表达式

javascript - JS 函数构造函数每次都重新解析?

html - 从 JSON 对象中的非结构化字符串中提取 html 标签和数据

c# - JSON 响应格式无效?

mysql - 如何在 MySQL 5.7 中的 JSON 数组中获取唯一/不同的元素

javascript - 来自 wordpress postmeta 表键的多个值

php - 如何跨多个 php 文件共享一个实例 a-la-Wordpress?

ruby - 无法用 ruby​​ 解析 json