简体   繁体   English

显示来自Wordpress RSS提要的媒体文件

[英]Display media file from Wordpress RSS feed

I am using the below code to grab data from a wordpress RSS feed. 我正在使用以下代码从wordpress RSS提要中获取数据。 However I am having an issue with grabing the 'media file' from the feed. 但是我从提要中获取“媒体文件”时遇到问题。

<?php
        $rss = new DOMDocument();
        $rss->load('http://www.ipa.co.uk/rss/jobs');
        $feed = array();
        foreach ($rss->getElementsByTagName('item') as $node) {
            $item = array ( 
                'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                'image' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
                );
            array_push($feed, $item);
        }
        $limit = 20;
        for($x=0;$x<$limit;$x++) {
            $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
            $link = $feed[$x]['link'];
            $description = $feed[$x]['desc'];
            $date = date('l F d, Y', strtotime($feed[$x]['date']));?>
            <div class="col-md-4 job-item">
                <div class="job-item__inner">
                    <?php
                    echo '<h2 class="job-title"><a href="'.$link.'" title="'.$title.'">'.$title.'</a></h2>';
                    echo '<small><em>Posted on '.$date.'</em></small></p>';
                    echo '<p>'.$description.'</p>';
                    echo '<img src="'.$image.'"/>';?>
                </div>
            </div>
        <?php } ?>

I have seen this answer on SO which helped me add in the getElementsByTagName('enclosure')->item(0)->getAttribute('url') . 我已经在SO上看到了这个答案 ,它帮助我添加了getElementsByTagName('enclosure')->item(0)->getAttribute('url') However its not displaying any img src. 但是,它不显示任何img src。 I think 'enclosure' is correct as I get errors if I change it to something like this getElementsByTagName('media')->item(0)->getAttribute('url') 我认为'附件'是正确的,因为如果将其更改为类似getElementsByTagName('media')->item(0)->getAttribute('url')则会出错

The issue must be that I'm targeting the wrong area of the feed. 问题必须是我定位的Feed区域错误。 But I'm not sure what to add to get the image displaying. 但是我不确定要添加什么来显示图像。

You should definitely enable NOTICES while developing with PHP. 使用PHP开发时,您绝对应该启用NOTICES This would have led you to the answer: Your extraction code is correct but in the for-loop you need an additional line 这将导致您得到答案:您的提取代码正确,但是在for循环中,您需要附加一行

$image = $feed[$x]['image'];

otherwise the variable $image is undefined n this context. 否则,变量$image在此上下文中未定义。


//edit: By the way the 2 loops are inefficient. // edit:顺便说一下,这两个循环效率很低。 Better use only the foreach loop and a counter. 最好仅使用foreach循环和计数器。 Once the counter is at 20, do a break; 一旦计数器到20, break;

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

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