• Hi – I’ve got a number of companies created as posts. I’m trying to add an RSS feed from an external website to each company page using custom fields. I’d like to use the same format as the default RSS widget so I’d like to copy the code from this to the single.php file if possible but then cutomise the feed source with a custom field on the post page. Does anyone know how I’d achieve this?

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Stefan

    (@stefan83)

    Ok, so I’m nearly there I’ve found the following code that pulls in an RSS feed:

    <?php include_once(ABSPATH.WPINC.'/rss.php');
    wp_rss('http://feedurl.com', 3); ?>

    but I’d like to replce the ‘http://feedurl.com&#8217; with a custom field like the code below:

    <?php if ( get_post_meta($post->ID, 'News Feed:') ) : ?>
     <?php echo get_post_meta($post->ID, 'News Feed:', true); ?>
    <?php endif;?>

    Can anyone help me with merging the two?

    Thanks

    Thread Starter Stefan

    (@stefan83)

    Anyone? Thanks

    Thread Starter Stefan

    (@stefan83)

    So judging by the deafening silence this is not possible?

    Thread Starter Stefan

    (@stefan83)

    Ok, I managed to get it working, for anyone who is interested…

    <?php // Get RSS Feed(s)
    include_once(ABSPATH . WPINC . '/rss.php');
    $feeds =  get_post_meta($post->ID, 'feed', true);
    $rss = fetch_rss($feeds);
    $maxitems = 5;
    $items = array_slice($rss->items,0,$maxitems);
    
    ?>
    <div id="mjrssfeed">
    <div id="mjrssfeedheader"><h2><?php the_title(); ?> News from Mining Journal</h2></div>
    <?php if (empty($items)) echo '<p>No items</p>';
    else
    foreach ( $items as $item ) : ?>
    <div class="mjrss">
    <h3><a href="<?php echo $item['link']; ?>" target="_blank"><?php echo $item['title']; ?></a></h3>
    <p><?php echo $item['description']; ?> <a href="<?php echo $item['link']; ?>" target="_blank">...read more</a></p>
    </div>
    <?php endforeach; ?>
    </div>

    Altough I’m getting an error message when the feed does not exist..

    Warning: array_slice() expects parameter 1 to be array, null given in /home/miningev/public_html/minesandmoney/london/wp-content/themes/XSWordPressTheme/single.php on line 44.

    Can anyone explain why?

    Thanks

    When the feed doesn’t load your array is not initialised you need to check that $rss->items is not null before calling slice.

    However I don’t do much PHP these days so couldn’t give you the exact syntax to do the above from the top of my head.

    something like

    if(!is_null($rss->items)) {
      $items = slice(...)
      } else {
      $items = NULL;
    }

    You’ll also probably need to change the test of $items for empty to a test for null instead. Not sure of the initialisation nuances of PHP.

    Thread Starter Stefan

    (@stefan83)

    Thanks rebelit, that’s really helpful

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display RSS feed on a post with custom fields’ is closed to new replies.