0

I am trying to display content in a wordpress post from an RSS feed. I'd like to be able to enter the RSS URL into a Wordpress post so that the feed content can change from post to post.

I can create the desired output using this code and by directly entering in an RSS url where is says 'ENTER RSS URL HERE'

    <?php
    $rss = new DOMDocument();
    $rss->load('ENTER RSS URL HERE');
    $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,
            );
        array_push($feed, $item);
    }
    $limit = 6;
    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']));
                echo '<div class="product-item">';
        echo '<p>'.$description.'</p>';
            echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
                echo '</div>';
    }
?>

And I can also output a URL entered as a post meta value like this:

<?php echo get_post_meta($post->ID, 'RSS', true); ?>

I'm just unsure how I can combine the two so that the RSS URL can be changed dynamically. Any help would be much appreciated.

3 Answers 3

0

You can try the following

<?php
global $post;
$url = ((filter_var(get_post_meta($post->ID, 'RSS', true), FILTER_VALIDATE_URL)) ? get_post_meta($post->ID, 'RSS', true) : 'fallback url');
$rss = new DOMDocument();
$rss->load($url);
$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,
    );
    array_push($feed, $item);
}
$limit = 6;
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']));
    echo '<div class="product-item">';
    echo '<p>' . $description . '</p>';
    echo '<p><strong><a href="' . $link . '" title="' . $title . '">' . $title . '</a></strong><br />';
    echo '</div>';
}
?>
1
  • Thank you Igor! It was working on the fall back URL only so I removed the FILTER_VALIDATE_EMAIL (not sure if I was meant to replace that with something?) and it's doing exactly what I need.
    – jkendall
    Jul 19, 2016 at 5:36
0

Here is the final code that I'm using which seems to be working perfectly thanks!

    <?php
global $post;
$url = ((filter_var(get_post_meta($post->ID, 'RSS', true))) ? get_post_meta($post->ID, 'RSS', true) : 'ENTER FALL BACK URL HERE');
$rss = new DOMDocument();
$rss->load($url);
$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,
    );
    array_push($feed, $item);
}
$limit = 6;
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']));
    echo '<div class="product-item">';
    echo '<p>' . $description . '</p>';
    echo '<p><strong><a href="' . $link . '" title="' . $title . '">' . $title . '</a></strong><br />';
    echo '</div>';
}
?>
0

If the RSS-URL is always inside the post metadata, you could simply use that:

$rss->load(get_post_meta($post->ID, 'RSS', true));

Or do I get something wrong on your question?

2
  • Thanks Christian - that is what I want to achieve however it doesn't seem to work... am I missing something else? When I enter this in dreamweaver it says there's a syntax error on this line. When I try it on Wordpress is breaks the post loop and no content displays....
    – jkendall
    Jul 19, 2016 at 5:23
  • @jkendall Sorry, there was a typo. I accidentally typed a semicolon to much. Fixed it now.
    – Christian
    Jul 21, 2016 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.