如何导出WordPress帖子?

时间:2010-08-31 07:02:21

标签: php xml wordpress export

如何将WordPress帖子导出为XML或CSV?我正在寻找一种巧妙的方法,在PHP的帮助下完成这项工作。

注意:我不想通过管理面板进行操作,因为我想自动化它。

3 个答案:

答案 0 :(得分:3)

要从PHP中执行此操作,请执行以下操作:

  1. 从数据库中获取标记为publish的所有帖子。
  2. 使用以下array2xml函数将它们导出到数组:
  3. <?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 :(得分:1)

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

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

答案 2 :(得分:0)

根据Wordpress blog...

  

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