gpt4 book ai didi

json - 将 Ghost 导出到 Wordpress

转载 作者:行者123 更新时间:2023-12-01 04:46:07 28 4
gpt4 key购买 nike

我一直在寻找将我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com