【发布时间】:2021-05-30 09:51:37
【问题描述】:
我一直在寻找将我的 Ghost 博客文章复制到 Wordpress 的可能性。
到目前为止,我已经成功地将所有幽灵数据导出到一个 JSON 文件——你知道任何现有的工具可以将它转换为 Wordpress 可以导入的东西吗?
如果不是,我必须自己构建一些东西,您是否建议将 JSON 解析为 WXR 文件或类似文件,或者直接导入 Wordpress 的数据库?
提前致谢! K.
【问题讨论】:
我一直在寻找将我的 Ghost 博客文章复制到 Wordpress 的可能性。
到目前为止,我已经成功地将所有幽灵数据导出到一个 JSON 文件——你知道任何现有的工具可以将它转换为 Wordpress 可以导入的东西吗?
如果不是,我必须自己构建一些东西,您是否建议将 JSON 解析为 WXR 文件或类似文件,或者直接导入 Wordpress 的数据库?
提前致谢! K.
【问题讨论】:
我将 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');
【讨论】:
after_setup_theme 钩子在页面加载时被调用(我确信有一个更聪明的方法来启动它)。它会检查 Wordpress 中是否已经存在帖子标题(以防止重复),但无论如何我都建议在导入完成后将其删除。请注意,如果您的博客有重复的帖子标题,则需要删除该检查(但请注意仅加载一次!)。是的,JSON 文件应该与 functions.php 位于同一文件夹中(或相应地更新路径)。
我的建议是使用 Google Refine 导入 JSON,导出 CSV,然后使用 WP Ultimate CSV Importer Plugin 将其导入您的 WordPress 网站。希望这可以帮助。
【讨论】:
这个问题已经很老了,但截至 2017 年,我仍然无法找到 Ghost > WP 帖子迁移的解决方案。我所做的是:
将一些 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
发布日期
删除其他 JSON 字段/结构,因此只有“帖子”:[]
使用 JSON 到 CSV 转换器,比如这个 - https://konklone.io/json/ 并下载 CSV 结果文件。
注意:
Ghost 不支持将图像与其他数据一起导出(截至 2017 年 1 月 31 日)。
您可能希望将“post_status”从“发布”更改为“待处理”,因此在正确编辑之前不会立即发布帖子;)
【讨论】:
如果有人在将来寻找这个:
您可以使用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的作者。
【讨论】:
我尝试了各种导入器,最终最好的解决方案是直接从 RSS URL 导入,例如 https://myghostblog.com/rss/ 和 import-xml-feed 插件。
附:记住分页。默认情况下,您的 RSS 链接可能仅显示帖子的第一页,因此要导入其他页面,您必须手动将 /2/ /3/ 添加到 URL 并分别导入。 (至少,这是我需要做的。)
【讨论】: