简体   繁体   English

将laravel博客内容导出到wordpress

[英]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 所以我们能做的就是

  1. Connect to the Laravel Database from your WordPress installation. 从WordPress安装连接到Laravel数据库。
  2. Select the Laravel db data using wpdb query ( https://codex.wordpress.org/Class_Reference/wpdb ) 使用wpdb查询选择Laravel数据库数据( https://codex.wordpress.org/Class_Reference/wpdb
  3. Insert into our WordPress site using wp_insert_post ( https://developer.wordpress.org/reference/functions/wp_insert_post/ ) 使用wp_insert_posthttps://developer.wordpress.org/reference/functions/wp_insert_post/ )插入我们的WordPress网站
  4. If there are pictures attached to the Laravel Blog you must query it as well and upload it and attach to the WordPress as well. 如果Laravel博客上附有图片,您也必须查询并上传并附加到WordPress。 ( https://codex.wordpress.org/Function_Reference/wp_insert_attachment ) https://codex.wordpress.org/Function_Reference/wp_insert_attachment

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.phpfooter.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM