PS1:第2回の修正により、現在ではカテゴリー、記事、タグの3つのデータ間移行に対応しています。
PS2:記事IDが常に一致しない場合は、WordPressの「記事カテゴリ」データテーブルおよび関連テーブルをクリアする試みをお試しください。
以前はブログにtypechoを使っていたが、その後帝国に移行しました。しかし、帝国の機能が多すぎるため、ブログで帝国を使うのは、やや大材小用な感じがします!
それに、ブログでは、重要なコードを頻繁に記録しています。それらのコードは、すべて私が心血を注いで書いたものです。どう言おうと、この苦労が無駄だったはずがありませんよね?
そこで知識の有料化を考えたが、帝国というテーマではもうわざわざ手を動かしたくないので、結局WordPressに移すことにした!
結局のところ、WordPressはブログに適しているからです……ここで言うのは、帝国がウェブサイトの構築に適していないということではありません。
以下に、移行に関する詳細な記録とコードをまとめます。
ステップ1:WordPressの構築
公式ウェブサイトからWordPressをダウンロードすればよいので、インストールは通常の方法で進めてください。
ステップ2:データインポートツールをインストールする

ステップ3:帝国のデータをエクスポートする
ここでデータを出力するには、コードを使用する必要があります。記事とカテゴリーのみを出力する場合、カテゴリーの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[[email protected]]]></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;
?>私のブログのデータは比較的少ないので、約数千件程度です。そのため、一度に直接インポートしました。もしあなたのウェブサイトのデータが非常に多ければ、コードを無料で修正してもらうよう、ぜひ私に連絡してください!
ステップ4:WordPressの導入

これにより、帝国のデータをWordPressに移行できます。