【问题标题】:Display media files from a wordpress rss feed显示来自 wordpress RSS 提要的媒体文件
【发布时间】:2012-12-18 07:15:23
【问题描述】:

我正在使用 PHP 解析常规 wordpress.com 博客的 rss 提要,以便在我的网站上显示帖子的预览。

它正确显示:标题、描述和出版日期。但我还想显示每个帖子的图像。如果我打开提要的 URL,会有一个带有“媒体文件链接”的图表,但我不知道如何访问这些链接。

你有什么建议吗?

这是我正在使用的代码:

<?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>  

非常感谢您的帮助

【问题讨论】:

    标签: php wordpress rss


    【解决方案1】:

    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";
        }
    }
    

    密钥是getElementsByTagNameNS。它让您可以使用 media 命名空间,以便您可以抓取内容。

    【讨论】:

    • RSS 提供了它,但我不知道如何访问它
    猜你喜欢
    • 2015-12-22
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2012-11-07
    • 1970-01-01
    • 2015-04-11
    • 2017-11-12
    相关资源
    最近更新 更多