Search code examples
feedcustom-fieldssimplepie

Calling a field inside included feed


I'm trying to do different feeds into one area that I switch with jquery tabs. I have a feed template and only need to switch the rss feed url inside of it each time. I tried to set a value to the url on my index page and then echo it inside the feed but I'm obviously not doing this right. Whats wrong with my approach?

<----Index Page--->
<div class="news-feed"><?php $url = "http://mysite.com/category/news/feed/"; ?><?php locate_template( array( 'feeds/news-feed.php' ), true ) ?></div>

<-------FEED-------->
<?php // Get RSS Feed(s)
 include_once(ABSPATH . WPINC . '/feed.php');

 // Get a SimplePie feed object from the specified feed source.
   $rss = fetch_feed('$url');<---WHERE I NEED MY CUSTOM URL
   if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
// Figure out how many total items there are, but limit it to 5. 
   $maxitems = $rss->get_item_quantity(5); 

// Build an array of all the items, starting with element 0 (first element).
   $rss_items = $rss->get_items(0, $maxitems); 
    endif;
    ?>

   <ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else
  // Loop through each feed item and display each item as a hyperlink.
    foreach ( $rss_items as $item ) : ?>
      <li>
        <a href='<?php echo esc_url( $item->get_permalink() ); ?>'
        title='<?php echo esc_html( $item->get_title() ); ?>'>
        <?php echo '<img src="' .get_first_image_url($item->get_content()). '"/>'; ?>
        <?php echo esc_html( $item->get_title() ); ?></br>
        <span style="color:#e2e2e2"><?php echo shorten( $item->get_description (),140 ); ?></span></a>
     </li>
 <?php endforeach; ?>
</ul>

Solution

  • You need to change

    fetch_feed('$url');
    

    to

    fetch_feed($url);
    

    Single quoted strings in PHP do not have variables inside them expanded.