如何导出 WordPress 帖子?

发布于 2024-09-16 11:31:46 字数 120 浏览 5 评论 0原文

如何将 WordPress 帖子导出为 XML 或 CSV?我正在寻找一种在 PHP 的帮助下实现这一点的巧妙方法。

注意:我不想通过管理面板执行此操作,因为我想使其自动化。

How to export WordPress posts to XML or CSV? I am looking for a neat way to do it with the help of PHP.

Note: I don't want to do it via the admin panel because I want to automate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喵星人汪星人 2024-09-23 11:31:46

要从 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>";
?>

To do it from PHP, do it like this:

  1. Get all posts marked publish from the database.
  2. Export them to an array by using following array2xml function:

.

<?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>";
?>
空名 2024-09-23 11:31:46

在您的 WordPress 设置中,请查看 wp-admin/export.php 第 28-48 行(在 3.0 设置中)。
这是生成可在管理中下载的 XML 文件的代码。您也许可以在自己的代码中使用它(不幸的是,它没有组织成函数,因此您必须进行一些复制粘贴)。

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

On your Wordpress setup, take a look at wp-admin/export.php lines 28-48 (on a 3.0 setup).
This is the code that generates the XML file downloadable in the admin. 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. You'll have to deal with inputting your credentials for that, though.

寄人书 2024-09-23 11:31:46

好吧,根据 Wordpress 博客...

您将找到导出和导入
现在选项位于您的“管理”下
博客管理区。 XML 格式是一种
RSS 2.0 的扩展版本,它
将被内置到下一个
WordPress 的可下载版本
(2.1)。

Well according to the Wordpress blog...

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文