保存自定义帖子类型后,我想使用 WordPress 向 XML 文件添加新行。我对如何添加新行但每行有多个属性感到困惑。我的XML文件的每一行如下
<markers>
<marker name="" lat="" lng="" type="" address="" phone="" hours1="" />
<marker name="" lat="" lng="" type="" address="" phone="" hours1="" />
<marker name="" lat="" lng="" type="" address="" phone="" hours1="" />
</markers>
对于我保存的每个新商店,我希望它将所有数据添加到一个新行中。我不知道我是否需要创建一个新的孩子然后添加它,因为我以前从未真正将数据添加到 XML 文件中,令人惊讶的是。我当前文件的代码如下
$xml = new SimpleXMLElement('<xml/>');
$stores = get_posts( array( 'post_type'=>'store', 'numberposts'=>-1 ) );
$xml->addChild('markers');
$name = $_POST['post_name'];
$lat = $_POST['wpcf']['latitude'];
$lng = $_POST['wpcf']['longitude'];
$type = $_POST['wpcf']['features'];
$address = $_POST['wpcf']['address'];
$phone = $_POST['wpcf']['telephone'];
$hours1 = $_POST['wpcf']['mon-fri'] . ", " . $_POST['wpcf']['saturday'] . ", " . $_POST['wpcf']['sunday'] ;
$xml->stores->addChild('marker');
$xml->stores->addChild('name', $name);
$xml->stores->addChild('lat', $lat);
$xml->stores->addChild('lng', $lng);
$xml->stores->addChild('type', $type);
$xml->stores->addChild('address', $address);
$xml->stores->addChild('phone', $phone);
$xml->stores->addChild('hours1', $hours1);
$file = SITE_URL() . '/testing.xml';
$open = fopen($file, 'w') or die ("File cannot be opened.");
fwrite($open, $xml->asXML());
fclose($open);