WordPress.org

Ready to get started?Download WordPress

Forums

Display RSS feed on a post with custom fields (7 posts)

  1. Stefan83
    Member
    Posted 3 years ago #

    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

  2. Stefan83
    Member
    Posted 3 years ago #

    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

  3. Stefan83
    Member
    Posted 3 years ago #

    Anyone? Thanks

  4. Stefan83
    Member
    Posted 3 years ago #

    So judging by the deafening silence this is not possible?

  5. Stefan83
    Member
    Posted 3 years ago #

    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

  6. rebelit
    Member
    Posted 3 years ago #

    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.

  7. Stefan83
    Member
    Posted 3 years ago #

    Thanks rebelit, that's really helpful

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.