PS1: 두 번째 수정으로 현재 분류, 기사, 태그 세 가지 데이터의 전송을 지원합니다.
PS2: 게시글 ID가 계속 일치하지 않는 경우, WordPress의 게시글 카테고리 데이터 테이블과 관계 테이블을 모두 지우어 보시기 바랍니다.
예전 블로그는 typecho를 사용했지만, 이후 Empire로 이전했습니다. 그런데 Empire의 기능이 너무 많아서 블로그에 이걸 쓰는 건 마치 큰 도구를 작은 일에 쓰는 듯한 느낌이 듭니다!
게다가 저는 블로그를 통해 중요한 코드를 자주 기록합니다. 이 코드는 제가 정말 열심히 작성한 것들이기 때문에, 아무리 노력해도 헛된 노력이 아니겠죠?
그래서 저는 지식 유료화라는 아이디어를 떠올렸지만, ‘임페리얼’을 계속 다루고 싶지 않아서 그냥 WordPress로 옮기기로 했습니다!
어쨌든 wordpress는 블로그에 더 적합합니다……이 말은 Empire가 웹사이트 구축에 부적합하다는 뜻은 아닙니다.
다음의 이전 작업 상세 기록과 코드를 정리합니다.
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로 이전할 수 있습니다.