如何将WordPress帖子导出为XML或CSV?我正在寻找一种巧妙的方法,在PHP
的帮助下完成这项工作。
注意:我不想通过管理面板进行操作,因为我想自动化它。
答案 0 :(得分:3)
要从PHP中执行此操作,请执行以下操作:
publish
的所有帖子。
<?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)
你会找到导出和导入 现在在您的“管理”下的选项 博客管理区。 XML格式是一种 RSS 2.0的扩展版本,以及它 将建成下一个 可下载的WordPress版本 (2.1)。