This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

The RSS displayer block is a great tool for pulling in feeds to your web page, but it does not give you the option to display images. Many RSS feeds have an associated thumbnail image. You can use SImple Pie documentation to bring in media elements or additional item tags, but there is no easy solution for pulling in a single graphic from the item content.

For example, I have an RSS feed that looks something like this:

    <item>
      <title>Generic Feed Title</title>
      <link>http://www.google.com</link>
      <description>
        <![CDATA[
         Brief description of feed...
        ]]>
      </description>
      <content:encoded>
        <![CDATA[
          <p><img src="http://www.google.com/someimage.jpg"/></p>
<p>Generic paragraph content of RSS feed...</p>
         ]]>
       </content:encoded>
    </item>

If I want to grab the first image that appears in my post, I need to extract the img tag and its contents. First things first, copy view.php from concrete/blocks/rss_displayer to blocks/rss_displayer. Next, edit your copy of view.php with the following code:

<div class="rssItemThumbnail">
            <?php 

            if (!function_exists('get_first_image_url')) 
            {
            function get_first_image_url($html) 
                {
                      {
                      if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) 
                        {
                        return $matches[1];
                        }
                         else echo '';
                      }

                }
            }
                echo '<img src="' .get_first_image_url($item->get_content()). '" />';

            ?>
            </div>

I recommend placing this code between the rssItem div tag and the rssItemTitle div tag:

<div class="rssItem">

...insert here! ....


  <div class="rssItemTitle">

                <a href="<?php echo  $item->get_permalink(); ?>" <?php  if($rssObj->launchInNewWindow) echo 'target="_blank"' ?> >
                    <strong><?php echo  $item->get_title(); ?></strong>
                </a>
            </div>
            <div class="rssItemDate"><em><?php echo  $item->get_date($dateFormat); ?></em></div>

The function get_first_image_url looks at your RSS feed and if it finds an HTML tag matching img, it extracts that tag and its contents and returns $matches. If you want to return more than one image, change the number behind $matches from [1] to however many images you want to pull.

In the echo statement, be sure to direct the function to pull from the content of your RSS item:

get_first_image_url($item->get_content())

This will work on multiple feeds as long as each feed is being displayed in the RSS Displayer block!

Loading Conversation