从emlog转到WordPress
07 / 10 / 2010 buling

        昨天晚上赶到4点钟,终于把emlog转换到WordPress。之前也在网上搜索了从emlog到WordPress的转换方法,貌似都不怎么好,就自己手动写了段php先把文章签过来再说,因为只是为了救急,也就没有分什么类什么的,代码试下

$id, 'post_author' => 1, 'post_date'=>$date, 'post_date_gmt'=>$date, 'post_content' => $content,
				'post_title' => $post_title, 'post_excerpt' => $excerpt, 'post_status' => 'publish', 'comment_status'=>'open',
				'ping_status' => 'open', 'post_password' => '', 'post_name'=>$post_name, 'to_ping' =>  '', 'pinged' => '',
				'post_modified'=>$date, 'post_modified_gmt'=>$date, 'post_content_filtered' => '', 'post_parent' => $v['sortid'],
				'guid' => '', 'menu_order' => 0, 'post_type' => 'post', 'comment_count'=>$comnum
				);
		$fields=array_keys($defaults);
		$formatted_fields=array_values($defaults);
		$sql = "INSERT INTO `wp_posts` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
		$flag=false;
		$flag=mysql_query($sql);
		if($flag){
			echo 'insert scuess for id='.$id.'
'; }else{ echo 'insert fill for id='.$id.'
'; } } } /** * 生成post_name函数,采用google翻译 * @param unknown_type $a * @return unknown */ function tran($a){ $url = "http://translate.google.com.hk/translate_a/t?client=t&text={$a}&sl=zh-CN&tl=en&pc=0"; $cl = curl_init( $url); curl_setopt( $cl , CURLOPT_RETURNTRANSFER,true); curl_setopt( $cl, CURLOPT_USERAGENT , 'Mozilla/5.0'); $res = curl_exec($cl); curl_close( $cl); $res=explode('"', $res); $res=isset($res[1]) ? $res[1] : $a; return str_replace(array('\\','"',"'"), array('','',''), $res); } /** * 更新文章分类代码 * @param unknown_type $emlog * @param unknown_type $conn */ function updateSort($emlog, $conn){ mysql_select_db('wordpress', $conn); foreach ($emlog as $v){ $id=$v['gid']; $sid=$v['sortid']; $sid = $sid==2 ? 4 : $sid; $sql="insert into `wp_term_relationships` values ('".implode( "','",array($id, $sid, 0))."')"; $flag=false; $flag=mysql_query($sql); if($flag){ echo 'insert scuess for id='.$id.'
'; }else{ echo 'insert fill for id='.$id.'
'; } } } $emlog=getEmlog($conn); insert($emlog, $conn); //updateSort($emlog, $conn); //建议不要同时更新 ?>

        因为需要翻译标题缘故,为了不出现超时,建议每次20条更新一次,修改select的limite就可以了。如第一次limit 0,20,第二次就改为limit 20,20。不懂sql的limit用法我就解释一下,第一个参数是偏移量,第二个是条数。
        除此,WordPress默认会把两个</br>转换为一个<p>的解决方法是:打开并编辑 wp-includes/formatting.php 文件,找到以下代码注释掉即可:

$pee = preg_replace('|
\s*
|', "\n\n", $pee); //$pee = preg_replace('|
\s*
|', "\n\n", $pee);

        最后说一下,为了方便以后插入html,php,python,css,js等代码,对编辑器稍作改变,添加相关按钮,这样不用每次手敲了,呵呵:

edButtons[edButtons.length]=new edButton("ed_pre_html","html","<0pre lang='html' colla='+'>\n","\n","p",-1);
edButtons[edButtons.length]=new edButton("ed_pre_php","php","<0pre lang='php' colla='+'>\n","\n","pp",-1);
edButtons[edButtons.length]=new edButton("ed_pre_python","python","<0pre lang='python' colla='+'>\n","\n","ppp",-1);
edButtons[edButtons.length]=new edButton("ed_pre_css","css","<0pre lang='css' colla='+'>\n","\n","pppp",-1);
edButtons[edButtons.length]=new edButton("ed_pre_js","js","<0pre lang='JavaScript' colla='+'>\n","\n","ppppp",-1);