[英]Exporting a laravel blog content to wordpress
我有一个内置laravel的博客,但我想将所有内容移动到wordpress中构建的现有博客,我已经尝试将数据库导出为CSV文件以导入我的wordpress数据库,但它不是以相同的表格格式,任何关于如何导入内容的想法
It is less likely to work the direct import since the table structures will be different. 由于表结构不同,因此不太可能直接导入。 So what we can do is to
所以我们能做的就是
wpdb
query ( https://codex.wordpress.org/Class_Reference/wpdb ) wpdb
查询选择Laravel数据库数据( https://codex.wordpress.org/Class_Reference/wpdb ) wp_insert_post
( https://developer.wordpress.org/reference/functions/wp_insert_post/ ) wp_insert_post
( https://developer.wordpress.org/reference/functions/wp_insert_post/ )插入我们的WordPress网站 You must take WordPress DB backup before doing this.
在执行此操作之前,您必须先备份WordPress数据库。
Sample code will be something like this. 示例代码将是这样的。
$mydb = new wpdb('username','password','laravel_database','localhost');
$rows = $mydb->get_results("select title, content from laravle_blogs_table");
foreach ($rows as $obj) :
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $obj->title ),
'post_content' => $obj->content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
endforeach;
You must change the field names as per your database.
您必须根据数据库更改字段名称。
This code can be put in any of the active theme files may be header.php
or footer.php
or other template and just load the page from your browser. 此代码可以放在任何活动主题文件中,可以是
header.php
或footer.php
或其他模板,只需从浏览器加载页面即可。 If there are a huge number of posts put a limit to the select query and insert step by step. 如果有大量帖子限制了选择查询并逐步插入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.