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’ 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)
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