Importing an RSS feed into post whilst using a custom field? (3 posts)

  1. dbramley
    Posted 4 years ago #

    Hope you can help with something. I currently have this code in my posts page:

    <?php if(function_exists('fetch_feed')) {
    	include_once(ABSPATH . WPINC . '/feed.php');               // include the required file
            $feed = fetch_feed('RSS FEED ADDRESS HERE'); // specify the source feed
    	$limit = $feed->get_item_quantity(7); // specify number of items
    	$items = $feed->get_items(0, $limit); // create an array of items
    	$feed->set_cache_duration (43200);
    if ($limit == 0) echo '<div>The feed is either empty or unavailable.</div>';
    else foreach ($items as $item) : ?>
    	<b><?php echo $item->get_title(); ?></b>
    	<?php echo substr($item->get_description(), 0, 200); ?>
    <?php endforeach; ?>

    This works great - simply enter a valid RSS feed into the place marked 'RSS FEED ADDRESS HERE' and it will display on the post.

    What I would like your help with is making the 'RSS FEED ADDRESS' pull in the address from a custom field.

    So, say I have two posts, both need to pull in a different RSS Feed.

    I have a custom field named RSS and I've entered the RSS feed url in there.

    How can I have it populate the code?

    I can pull in the custom field no problem using`<?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'RSS', true);

    I just don't know how to get it into that code.

    I will then wrap an if statement around the whole thing so that if there is no custom field named RSS it won't display.

    Any help greatly appreciated.


  2. dbramley
    Posted 4 years ago #

    If it helps I have now inserted a function

    function get_custom_field_data($key, $echo = false) {
    	global $post;
    	$value = get_post_meta($post->ID, $key, true);
    	if($echo == false) {
    		return $value;
    	} else {
    		echo $value;

    So that all I need do now is have`<?php {
    get_custom_field_data('RSS', true);
    } ?>`

    Inserted into my post template to have the custom field displayed on the page.

    I still can't get it to work within my fetch_feed function though :(


  3. dbramley
    Posted 4 years ago #

    managed to get this working and here is the code if anyone needs it:

    <?php if(get_post_meta($post->ID, "Address", true)): ?>
    <?php {
    $my_custom_field = get_post_meta($post->ID, "RSS", true); 
    	$feed = fetch_feed(get_post_meta($post->ID, "RSS")); // specify the source feed
    	$feed->set_cache_duration (43200);
    	$limit = $feed->get_item_quantity(10); // specify number of items
    	$items = $feed->get_items(0, $limit); // create an array of items
    foreach ($items as $item) : ?>
    	<p><a href="<?php echo $item->get_permalink(); ?>"
    	  title="<?php echo $item->get_date('j F Y @ g:i a'); ?>">
    		<?php echo $item->get_title(); ?>
    	</a><br />
    	<?php echo strip_tags($item->get_description()); ?>
    <?php endforeach; ?>
    <?php endif; ?>

    Basically I have a custom field named RSS - in the custom field I can then place an RSS feed url. If this doesnt exist then nothing is displayed.

    I've also stripped tags from my HTML code as one of the feeds I was importing contained code that screwed up my site.

Topic Closed

This topic has been closed to new replies.

About this Topic