简体   繁体   English

显示来自wordpress rss feed的媒体文件

[英]Display media files from a wordpress rss feed

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. 我正在使用PHP解析常规wordpress.com博客的RSS订阅源,以便在我的网站上显示帖子的预览。

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. 如果我打开提要的URL,则会有一个带有“媒体文件链接”的图表,但我不知道如何访问这些链接。

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

<?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 . 关键是getElementsByTagNameNS It let's you use the media namespace so you can snag the content. 它让你使用media命名空间,这样你就可以抓住内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM