简体   繁体   English

如何导出WordPress帖子?

[英]How to export WordPress posts?

How to export WordPress posts to XML or CSV? 如何将WordPress帖子导出为XML或CSV? I am looking for a neat way to do it with the help of PHP . 我正在寻找一种巧妙的方法来帮助PHP

Note: I don't want to do it via the admin panel because I want to automate it. 注意:我不希望通过管理面板执行此操作,因为我想自动执行它。

To do it from PHP, do it like this: 要从PHP中执行此操作,请执行以下操作:

  1. Get all posts marked publish from the database. 从数据库中获取所有标记为publish帖子。
  2. Export them to an array by using following array2xml function: 使用以下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>";
?>

On your Wordpress setup, take a look at wp-admin/export.php lines 28-48 (on a 3.0 setup). 在Wordpress设置中,查看wp-admin/export.php export.php第28-48行(在3.0设置上)。 This is the code that generates the XML file downloadable in the admin. 这是生成可在admin中下载的XML文件的代码。 You could maybe use that in your own code (unfortunately, it's not organized into a function, so you'll have to do some copy-paste-ing). 你可以在你自己的代码中使用它(不幸的是,它没有被组织成一个函数,所以你必须做一些复制粘贴)。

Also, you could automate the downloading of http://yourblog/wp-admin/export.php?download , as this URI would always redirect to a fresh XML export. 此外,您可以自动下载http://yourblog/wp-admin/export.php?download ,因为此URI将始终重定向到新的XML导出。 You'll have to deal with inputting your credentials for that, though. 但是,您必须处理输入凭据。

Well according to the Wordpress blog... 好吧根据Wordpress博客......

You'll find the export and import options now under “Manage” in your blog admin area. 您现在可以在博客管理区域的“管理”下找到导出和导入选项。 The XML format is an extended version of RSS 2.0, and it will be built into the next downloadable release of WordPress (2.1). XML格式是RSS 2.0的扩展版本,它将内置到WordPress(2.1)的下一个可下载版本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM