Support

Account

Home Forums General Issues Basic PHP question

Solved

Basic PHP question

  • <?php
    	include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    	$feed = fetch_rss(  the_field(blog_feed)  ); // specify feed url
    	$items = array_slice($feed->items, 0, 10); // specify first and last item
    ?>

    I’ve got several pages, each one fetches a different RSS feed. What is the proper syntaxis for inserting the_field(myfield) into $feed = fetch_rss, in the spot as shown above?

  • Turns out it pulls up the feed just as I entered above, but it doesn’t quite work:

    – If I hard-code a URL, it pulls up the feed items info
    – but when I use ACF, it prints the feed URL and then gives an error:

    Warning: array_slice() expects parameter 1 to be array, null given in myfile.php

    It’s like it outputs the feed URL differently than a hardcoded one. get_field didn’t do anything at all.

    What’s the proper way to include feed URL in there so it would be processed as it should?

  • On the off chance that someone else may be looking for this, here’s my solution.

    It checks whether the field exists, then outputs 10 RSS feed items including title and full content.

    <?php $blogfeed = get_post_meta($post->ID, ‘blog_feed’, true);
    if($blogfeed) : ?>
    
    <?php
    include_once(ABSPATH.WPINC.’/rss.php’); // path to include script
    $feed = fetch_rss($blogfeed); // specify feed url
    $items = array_slice($feed->items, 0, 10); // specify first and last item
    ?>
    
    <?php if (!empty($items)) : ?>
    <?php foreach ($items as $item) : ?>
    <div class=”feed-item”>
    <h4>” target=”_blank”><?php echo $item['title']; ?></h4>
    <p><?php echo $item['atom_content']; ?></p>
    </div>
    <?php endforeach; ?>
    
    <?php endif; ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Basic PHP question’ is closed to new replies.