【问题标题】:Trouble getting the content when generating an XML file with Wordpress for Google Maps使用 Wordpress for Google Maps 生成 XML 文件时无法获取内容
【发布时间】:2012-03-16 10:14:49
【问题描述】:

我需要通过 Wordpress 生成一个 XML 文件,用于在 Google 地图上生成标记。 我修改了在这里找到的一个函数:Write to XML file using fopen in Wordpress

只要帖子被修改,该函数就会运行。

地图方面工作正常。我可以生成一个 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); 
}

} 

【问题讨论】:

    标签: php xml wordpress google-maps-api-3


    【解决方案1】:

    标题和内容已通过 get_posts() 提供给您。无需使用 get_the_title() 或 get_the_content()。 get_posts() 的返回值在get_post() documentation 中说明。这会向您显示可用的数据。

    试试这个。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多