[英]Trouble getting the content when generating an XML file with Wordpress for Google Maps
我需要通過Wordpress生成XML文件,以用於在Google Map上生成標記。 我修改了在這里找到的功能: 在Wordpress中使用fopen寫入XML文件
只要修改帖子,該函數就會運行。
地圖方面的工作正常。 我可以生成一個XML文件,該文件的每個條目均具有標題,經度和緯度,並且已在地圖上正確繪制了它們。
但是我似乎無法獲得帖子內容,我想將其用作地址。 我什至無法獲得內容以進行測試。 我嘗試過編碼html以防與XMl沖突,但是什么也沒有。 似乎只是沒有得到任何內容。 是的,盡管我願意接受我可能錯過了一些簡單的事情的想法,但我已經確保了帖子的內容:-p
我的功能如下。
add_action( 'save_post', 'markers_xml' );
function markers_xml(){
if ($_POST['post_type'] == 'places-to-eat')
{
$xml = new SimpleXMLElement('<xml/>');
$markers = get_posts( array( 'post_type'=>'places-to-eat', 'numberposts'=>-1 ) );
$xml->addChild('markers');
foreach($markers as $i=>$marker){
$name = get_the_title($marker->ID);
$address = get_the_content($marker->ID);
$address = htmlentities($address);
$lat = get_post_meta($marker->ID, 'the-lat', true);
$lng = get_post_meta($marker->ID, 'the-lng', true);
$xml->markers->addChild('marker');
$xml->markers->marker[$i]->addChild('name', $name);
$xml->markers->marker[$i]->addChild('address', $address);
$xml->markers->marker[$i]->addChild('lat', $lat);
$xml->markers->marker[$i]->addChild('lng', $lng);
}
$file = '/public_html/wp-content/uploads/test.xml';
$open = fopen($file, 'w') or die ("File cannot be opened.");
fwrite($open, $xml->asXML());
fclose($open);
}
}
標題和內容已經可以通過get_posts()獲得。 無需使用get_the_title()或get_the_content()。 get_posts()的返回值在get_post()文檔中說明 。 這將顯示可用數據。
嘗試這個。
foreach($markers as $i=>$marker){
//replace this
//$name = get_the_title($marker->ID);
//with this
$name = $marker->post_title;
//replace this
//$address = get_the_content($marker->ID);
//with this
$address = $marker->post_content;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.