PS1:第二次修改,目前已支持分類,文章,tag三者數據轉移
PS2:如果文章ID一直對不齊,可以嘗試清空WordPress的文章分類數據表,關係表
我之前博客用typecho,後來遷移到帝國,但是帝國功能太多了,博客用帝國有點大材小用的感覺!
再者,我用博客我常記錄一些重要的代碼,這些代碼都是我辛辛苦苦的寫的,怎麼說也不能白幸苦了吧?
於是我就想到知識付費,但是帝國的話,我又不想去折騰了,索性就搬到wordpress!
畢竟wordpress才適合博客......這裏並不是說帝國不適合建站
這裏總結以下遷移詳細記錄與代碼。
第一步:搭建wordpress
官網下載wordpress即可,安裝的時候正常安裝即可。
第二步:安裝導入數據的工具

第三步:導出帝國的數據
這裏導出數據就需要用到代碼了,只導出文章和分類,分類url如果規則的話,可以保留不變,如果每個分類下都有不同的url形式的話,那就不行了
在你帝國網站根目錄建立一個etow.php,然後將下面的代碼放進去,訪問這個PHP即可看到xml數據,這時候Ctrl+S保存,保存爲xml文件
🔒 隱藏內容:付費閱讀
<?php
header("Content-type:text/xml");
$site = "https://www.4s5.cn";//修改成自己的網站鏈接
$sitename = "站點名稱";//站點名稱,自己修改
echo
<<<EOD
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->
<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the "WordPress" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!-- on the site. For each author, you may choose to map to an -->
<!-- existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->
<!-- generator="WordPress/6.1.1" created="2023-02-05 09:40" -->
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.2/"
>
<channel>
<title>{$sitename}</title>
<link>{$site}</link>
<description></description>
<pubDate>Sun, 05 Feb 2023 09:40:05 +0000</pubDate>
<language>zh-CN</language>
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>{$site}</wp:base_site_url>
<wp:base_blog_url>{$site}</wp:base_blog_url>
<wp:author><wp:author_id>1</wp:author_id><wp:author_login><![CDATA[1810216796]]></wp:author_login><wp:author_email><![CDATA[1810216796@qq.com]]></wp:author_email><wp:author_display_name><![CDATA[1810216796]]></wp:author_display_name><wp:author_first_name><![CDATA[]]></wp:author_first_name><wp:author_last_name><![CDATA[]]></wp:author_last_name></wp:author>
EOD;
require('e/class/connect.php'); //引入數據庫配置文件和公共函數文件
require('e/class/db_sql.php'); //引入數據庫操作文件
$link=db_connect(); //連接MYSQL
$empire=new mysqlquery(); //聲明數據庫操作類
$allcass = array();
$allcass_name = array();
//分類處理
$sql=$empire->query("SELECT classid,classpath,classname FROM `phome_enewsclass` where bclassid = 0"); //一級分類
while($r=$empire->fetch($sql)) //循環獲取查詢記錄
{
echo" <wp:category>
<wp:term_id>{$r['classid']}</wp:term_id>
<wp:category_nicename><![CDATA[{$r['classpath']}]]></wp:category_nicename>
<wp:category_parent><![CDATA[]]></wp:category_parent>
<wp:cat_name><![CDATA[{$r['classname']}]]></wp:cat_name>
</wp:category>
";
$allcass[$r['classid']] = $r['classpath'];
$allcass_name[$r['classid']] = $r['classname'];
$sql2=$empire->query("select classid,classpath,classname from {$dbtbpre}enewsclass where bclassid=$r[classid] "); //二級分類
while($s2=$empire->fetch($sql2)){
$s2['classpath'] = str_replace($r['classpath'].'/','',$s2['classpath']);
echo "<wp:category>
<wp:term_id>{$s2['classid']}</wp:term_id>
<wp:category_nicename><![CDATA[{$s2['classpath']}]]></wp:category_nicename>
<wp:category_parent><![CDATA[{$r['classpath']}]]></wp:category_parent>
<wp:cat_name><![CDATA[{$s2['classname']}]]></wp:cat_name>
</wp:category>
";
$allcass[$s2['classid']] = $s2['classpath'];
$allcass_name[$s2['classid']] = $s2['classname'];
$sql3=$empire->query("select classid,classpath,classname from {$dbtbpre}enewsclass where bclassid=$s2[classid] "); //二級分類
while($s3=$empire->fetch($sql3)){
$s3['classpath'] = str_replace($s2['classpath'].'/','',$s3['classpath']);
$allcass[$s3['classid']] = $s3['classpath'];
$allcass_name[$s3['classid']] = $s3['classname'];
echo "<wp:category>
<wp:term_id>{$s3['classid']}</wp:term_id>
<wp:category_nicename><![CDATA[{$s3['classpath']}]]></wp:category_nicename>
<wp:category_parent><![CDATA[{$s2['classpath']}]]></wp:category_parent>
<wp:cat_name><![CDATA[{$s3['classname']}]]></wp:cat_name>
</wp:category>
";
}
}
}
//TAG處理
$sql=$empire->query("SELECT * FROM `phome_enewstags`");
while($r=$empire->fetch($sql)) //循環獲取查詢記錄
{
$r['tagid'] = $r['tagid'] + 24;
echo "
<wp:tag>
<wp:term_id>{$r['tagid']}</wp:term_id>
<wp:tag_slug><![CDATA[{$r['tagname']}]]></wp:tag_slug>
<wp:tag_name><![CDATA[{$r['tagname']}]]></wp:tag_name>
</wp:tag>
<wp:term>
<wp:term_id>{$r['tagid']}</wp:term_id>
<wp:term_taxonomy><![CDATA[post_tag]]></wp:term_taxonomy>
<wp:term_slug><![CDATA[{$r['tagname']}]]></wp:term_slug>
<wp:term_parent><![CDATA[]]></wp:term_parent>
<wp:term_name><![CDATA[{$r['tagname']}]]></wp:term_name>
</wp:term>
";
$tagArray[$r['tagid']] = $r['tagname'];
}
//term處理
$sql=$empire->query("SELECT classid,classpath,classname FROM `phome_enewsclass` where bclassid = 0");
while($r=$empire->fetch($sql)) //循環獲取查詢記錄
{
echo" <wp:term>
<wp:term_id>{$r['classid']}</wp:term_id>
<wp:term_taxonomy><![CDATA[category]]></wp:term_taxonomy>
<wp:term_slug><![CDATA[{$r['classpath']}]]></wp:term_slug>
<wp:term_parent><![CDATA[]]></wp:term_parent>
<wp:term_name><![CDATA[{$r['classname']}]]></wp:term_name>
</wp:term>
";
$sql2=$empire->query("select classid,classpath,classname from {$dbtbpre}enewsclass where bclassid=$r[classid] ");
while($s2=$empire->fetch($sql2)){
$s2['classpath'] = str_replace($r['classpath'].'/','',$s2['classpath']);
echo "<wp:term>
<wp:term_id>{$s2['classid']}</wp:term_id>
<wp:term_taxonomy><![CDATA[category]]></wp:term_taxonomy>
<wp:term_slug><![CDATA[{$s2['classpath']}]]></wp:term_slug>
<wp:term_parent><![CDATA[{$r['classpath']}]]></wp:term_parent>
<wp:term_name><![CDATA[{$s2['classname']}]]></wp:term_name>
</wp:term>
";
$sql3=$empire->query("select classid,classpath,classname from {$dbtbpre}enewsclass where bclassid=$s2[classid] ");
while($s3=$empire->fetch($sql3)){
$s3['classpath'] = str_replace($s2['classpath'].'/','',$s3['classpath']);
echo "<wp:term>
<wp:term_id>{$s3['classid']}</wp:term_id>
<wp:term_taxonomy><![CDATA[category]]></wp:term_taxonomy>
<wp:term_slug><![CDATA[{$s3['classpath']}]]></wp:term_slug>
<wp:term_parent><![CDATA[{$r['classpath']}]]></wp:term_parent>
<wp:term_name><![CDATA[{$s3['classname']}]]></wp:term_name>
</wp:term>
";
}
}
}
echo ' <generator>https://wordpress.org/?v=6.1.1</generator>
';
//文章處理
$sql=$empire->query("SELECT * FROM `phome_ecms_news`"); //文章處理
while($r2=$empire->fetch($sql)) //循環獲取查詢記錄
{
// $r2['titleurl'] = str_replace('www','wp',$r2['titleurl']);
$time1 = date("H:i:s",$r2['lastdotime']);
$time2 = date("Y",$r2['lastdotime']);
$time3 = date("m",$r2['lastdotime']);
$time4 = date("d",$r2['lastdotime']);
$title = urlencode($r2['title']);
$classname = $allcass_name[$r2['classid']];
$classpatch = $allcass[$r2['classid']];
// $markdown = $converter->convert($r2['newstext']);
$markdown = str_replace('<pre>','<pre class="enlighter-pre" mce-contenteditable="false"><code class="gl">',$r2['newstext']);
$markdown = str_replace('</pre>','</code></pre>',$markdown);
$markdown = str_replace(' class="title"','',$markdown);
$markdown = str_replace('\"','"',$markdown);
$markdown = str_replace('/d/file/','/wp-content/uploads/',$markdown);
$markdown = str_replace('<h2>個人備註:</h2>','<h4>個人備註:</h4>',$markdown);
$tags = '';
//tag查詢
$sql2 = $empire->query("SELECT * FROM `phome_enewstagsdata` WHERE `id` = {$r2['id']} ");
while($r3=$empire->fetch($sql2)){
$r3['tagid'] = $r3['tagid'] + 24;
$tags .= '<category domain="post_tag" nicename="'.$tagArray[$r3['tagid']].'"><![CDATA['.$tagArray[$r3['tagid']].']]></category>';
}
echo
<<<EOD
<item>
<title><![CDATA[{$r2['title']}]]></title>
<link>{$r2['titleurl']}</link>
<pubDate>Sun, {$time4} Feb {$time2} {$time1} +0000</pubDate>
<dc:creator><![CDATA[1810216796]]></dc:creator>
<guid isPermaLink="false">{$site}/?p={$r2['id']}</guid>
<description></description>
<content:encoded><![CDATA[{$markdown}]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>{$r2['id']}</wp:post_id>
<wp:post_date><![CDATA[{$time2}-{$time3}-{$time4} {$time1}]]></wp:post_date>
<wp:post_date_gmt><![CDATA[{$time2}-{$time3}-{$time4} {$time1}]]></wp:post_date_gmt>
<wp:post_modified><![CDATA[{$time2}-{$time3}-{$time4} {$time1}]]></wp:post_modified>
<wp:post_modified_gmt><![CDATA[{$time2}-{$time3}-{$time4} {$time1}]]></wp:post_modified_gmt>
<wp:comment_status><![CDATA[open]]></wp:comment_status>
<wp:ping_status><![CDATA[open]]></wp:ping_status>
<wp:post_name><![CDATA[{$title}]]></wp:post_name>
<wp:status><![CDATA[publish]]></wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type><![CDATA[post]]></wp:post_type>
<wp:post_password><![CDATA[]]></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
{$tags}
<category domain="category" nicename="{$classpatch}"><![CDATA[{$classname}]]></category>
<wp:postmeta>
<wp:meta_key><![CDATA[_edit_last]]></wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key><![CDATA[_wp_page_template]]></wp:meta_key>
<wp:meta_value><![CDATA[default]]></wp:meta_value>
</wp:postmeta>
</item>
EOD;
}
echo '
</channel>
</rss>
';
db_close(); //關閉MYSQL鏈接
$empire=null;
?>由於我博客數據比較少,大概幾千篇,所以直接一步就導出了,如果您網站數據過多,可以聯繫我免費修改代碼!
第四步:導入wordpress

這樣便可以把你帝國的數據轉移到wordpress裏面了