• Resolved sylvianNYC

    (@sylviannyc)


    Hi

    I’ve been playing around with this for over an hour and I’ve searched for clues but no luck. I think I’m just lacking some basic PHP knowledge to solve this.

    I have the following code working fine for me:

    <?php
    	include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    	$feed = fetch_rss('FEED-URL'); // specify feed url
    	$items = array_slice($feed->items, 0, 3); // specify first item and duration
    ?>
    <?php if (!empty($items)) : ?>
    <?php foreach ($items as $item) : ?>
    <a href="<?php echo $item['link']; ?>"><?php echo $item['description']; ?></a>
    <?php endforeach; ?>
    <?php endif; ?>

    This gives me three latest posts. How do I keep the selection to 3 items but have it be random posts?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Use shuffle().

    Try below code.

    <?php
    include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    $feed = fetch_rss('FEED-URL'); // specify feed url
    shuffle($feed->items);
    $items = array_slice($feed->items, 0, 3); // specify first item and duration
    
    if(!empty($items)){
    	foreach ($items as $item){
    		echo '<a href="'.$item['link'].'">'.$item['description'].'</a>';
    	}
    }
    ?>

    Thread Starter sylvianNYC

    (@sylviannyc)

    Genius! Works like a charm. One little line.
    I learn something every day.

    Domo arigato

    Hello
    Im having an issue with calling a custom field variable into the feed url.

    I basically want a page to show a different feed url based on whats been inputed into the custom field option. How would i implement this using this setup.

    <?php
    	   $feeds =  get_post_meta($post->ID, 'feeds',true);
    ?>
    <?php
    	include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    	$feed = fetch_rss('FEED-URL I WANT TO CALL FROM A CUSTOM FIELD'); // specify feed url
    	$items = array_slice($feed->items, 0, 3); // specify first item and duration
    ?>
    <?php if (!empty($items)) : ?>
    <?php foreach ($items as $item) : ?>
    <a href="<?php echo $item['link']; ?>"><?php echo $item['description']; ?></a>
    <?php endforeach; ?>
    <?php endif; ?>

    thanks in advanced to anyone with a solution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Random post from an RSS feed’ is closed to new replies.