如何从wordpress循环中的url获取xml数据?

时间:2015-09-28 09:36:54

标签: php xml wordpress

我正在尝试在wordpress recents主题循环中显示xml数据。我尝试了这个,但它没有在每篇文章中显示相同的输出。

$xml=simplexml_load_file("http://www.mywebsite.com/count.xml") or die("Error: Cannot create object");

                    foreach($xml->counts as $counts)
{
echo $counts->ss . "<br>";
}               

XML文件:

<gizcounts>

  <counts>
   <lkid>1</lkid>
   <sid>1</sid>
   <ss>0</ss>
 </counts>

 <counts>
   <lkid>2</lkid>
   <sid>3</sid>
   <ss>44</ss>
 </counts>

  <counts>
   <lkid>3</lkid>
   <sid>2</sid>
   <ss>35</ss>
 </counts>

</gizcounts>

我想知道如何在wordpress循环中获取xml数据。

1 个答案:

答案 0 :(得分:0)

尝试使用

$xml->children()

http://www.w3schools.com/php/func_simplexml_children.asp

在这种情况下

    $xml=simplexml_load_file("http://www.mywebsite.com/count.xml") or die("Error: Cannot create object");

                foreach($xml->children() as $counts)


  {
echo $counts->ss . "<br>";
}