2010-08-31 60 views
1

如何将WordPress帖子导出为XML或CSV?我在PHP的帮助下寻找一个简单的方法来完成它。如何导出WordPress的帖子?

注意:我不想通过管理面板来做,因为我想自动化它。

回答

0

那么根据Wordpress blog...

你会找到出口,并在您的博客 管理区现在导入下的“管理”选项 。 XML格式是RSS 2.0的一个扩展版本 ,它将被构建到下一个 可下载版本的WordPress (2.1)中。

3

从PHP这样做,像这样做:

  1. 获取的所有帖子标志着从数据库publish
  2. 它们导出到使用以下array2xml函数数组:

<?php 
function array2xml($array, $name='array', $standalone=TRUE, $beginning=TRUE) 
{ 
    global $nested; 

    if ($beginning) 
    { 
     if ($standalone) header("content-type:text/xml;charset=utf-8"); 
     $output .= '<'.'?'.'xml version="1.0" encoding="UTF-8"'.'?'.'>' . PHP_EOL; 
     $output .= '<' . $name . '>' . PHP_EOL; 
     $nested = 0; 
    } 

    // This is required because XML standards do not allow a tag to start with a number or symbol, you can change this value to whatever you like: 
    $ArrayNumberPrefix = 'ARRAY_NUMBER_'; 

    foreach ($array as $root=>$child) 
    { 
     if (is_array($child)) 
     { 
      $output .= str_repeat(" ", (2 * $nested)) . ' <' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . PHP_EOL; 
      $nested++; 
      $output .= array2xml($child,NULL,NULL,FALSE); 
      $nested--; 
      $output .= str_repeat(" ", (2 * $nested)) . ' </' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . PHP_EOL; 
     } 
     else 
     { 
      $output .= str_repeat(" ", (2 * $nested)) . ' <' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '><![CDATA[' . $child . ']]></' . (is_string($root) ? $root : $ArrayNumberPrefix . $root) . '>' . PHP_EOL; 
     } 
    } 

    if ($beginning) 
     $output .= '</' . $name . '>'; 

    return $output; 
} 

//connect to database and select database (edit yourself) 
mysql_connect("localhost", "username", "password"); 
mysql_select_db("databasename"); 

//Get all posts whose status us published. 
$result = mysql_query("SELECT * FROM wp_posts WHERE post_status = 'publish'"); 
while($row = mysql_fetch_assoc($result)) 
    $posts[] = $row; 

//convert to array and print it on screen: 
echo "<pre>"; 
echo htmlentities(array2xml($posts, 'posts', false)); 
echo "</pre>"; 
?> 
1

在你的WordPress安装,看看wp-admin/export.php线28-48(在3.0设置)。 这是生成管理中可下载的XML文件的代码。你也许可以在你自己的代码中使用它(不幸的是,它没有组织成一个函数,所以你必须做一些复制粘贴)。

此外,您可以自动下载http://yourblog/wp-admin/export.php?download,因为此URI始终会重定向到新的XML导出。不过,你必须处理输入你的凭证。