显示来自 wordpress RSS 提要的媒体文件 [英] Display media files from a wordpress rss feed
问题描述
我正在使用 PHP 解析常规 wordpress.com 博客的 RSS 提要,以便在我的网站上显示帖子的预览.
它正确显示:标题、描述和出版日期.但我还想显示每个帖子的图片.如果我打开提要的 URL,会看到一个带有媒体文件链接"的图表,但我不知道如何访问这些链接.
您有什么建议吗?
这是我使用的代码:
load($xml);$items=$xmlDoc->getElementsByTagName('item');$max_items= 15;?><?php foreach( $items as $i => $item ):?><?phpif($i>=$max_items) 中断;$title = $item->getElementsByTagName("title")->item(0)->nodeValue;$description = $item->getElementsByTagName("description")->item(0)->nodeValue;$data = $item->getElementsByTagName("pubDate")->item(0)->nodeValue;?><div class="posts"><div class="rssentry"><h2><?php echo $title?></h2><div class="rsscontent"><?php echo html_entity_decode($description)?></div><div class="metadata"><?php echo $data?></div>
<?php endforeach;?>
非常感谢您的帮助
PHP
load('http://testmustard.wordpress.com/feed/');$NodeList = $Document->getElementsByTagName('item');$i = 0;foreach ($NodeList 作为 $Node) {如果 ($i++ >= $maxItems) {休息;}$media = $Node->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content');如果 ($media-> 长度 > 0) {$imageUrl = $media->item(0)->getAttribute('url');echo "$imageUrl\n";} 别的 {echo "无媒体:内容\n";}}
关键是getElementsByTagNameNS
.它让您可以使用 media
命名空间,以便您可以获取内容.
I am parsing the rss feed of a regular wordpress.com blog using PHP in order to display a preview of the posts on my website.
It correctly displays: Title, Description and publication date. BUT I would like to display also the images of each post. If I open the feed's URL, there is a chart with "media files links", but I don't know how to access those links.
Do you have any suggestions?
This is the code I am using:
<?php
$xml=("http://testmustard.wordpress.com/feed/");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$items=$xmlDoc->getElementsByTagName('item');
$max_items= 15;
?>
<?php foreach( $items as $i => $item ):?>
<?php
if($i>=$max_items) break;
$title = $item->getElementsByTagName( "title" )->item(0)->nodeValue;
$description = $item->getElementsByTagName( "description" )->item(0)->nodeValue;
$data = $item->getElementsByTagName( "pubDate" )->item(0)->nodeValue;
?>
<div class="posts">
<div class="rssentry">
<h2><?php echo $title?></h2>
<div class="rsscontent"><?php echo html_entity_decode($description)?></div>
<div class="metadata"><?php echo $data?></div>
</div>
</div>
<?php endforeach;?>
</div>
Thanks a lot for your help
PHP
<?php
$maxItems = 15;
$Document = new DOMDocument();
$Document->load('http://testmustard.wordpress.com/feed/');
$NodeList = $Document->getElementsByTagName('item');
$i = 0;
foreach ($NodeList as $Node) {
if ($i++ >= $maxItems) {
break;
}
$media = $Node->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content');
if ($media->length > 0) {
$imageUrl = $media->item(0)->getAttribute('url');
echo "$imageUrl\n";
} else {
echo "No media:content\n";
}
}
The key is getElementsByTagNameNS
. It let's you use the media
namespace so you can snag the content.
这篇关于显示来自 wordpress RSS 提要的媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!