[英]How to export WordPress posts?
如何將WordPress帖子導出為XML或CSV? 我正在尋找一種巧妙的方法來幫助PHP
。
注意:我不希望通過管理面板執行此操作,因為我想自動執行它。
要從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>";
?>
在Wordpress設置中,查看wp-admin/export.php
export.php第28-48行(在3.0設置上)。 這是生成可在admin中下載的XML文件的代碼。 你可以在你自己的代碼中使用它(不幸的是,它沒有被組織成一個函數,所以你必須做一些復制粘貼)。
此外,您可以自動下載http://yourblog/wp-admin/export.php?download ,因為此URI將始終重定向到新的XML導出。 但是,您必須處理輸入憑據。
您現在可以在博客管理區域的“管理”下找到導出和導入選項。 XML格式是RSS 2.0的擴展版本,它將內置到WordPress(2.1)的下一個可下載版本中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.