• I have a strange problem with pulling in RSS entries from a feed. Most of the times I get this error on the page, but after reloading, it works. Obviously, the best thing would be for it never to show the error and just reload automatically when there was a problem.

    This is the error code i get in the browser:
    Warning: array_slice() expects parameter 1 to be array, null given in /home3/kasperso/public_html/wp-content/themes/mechatronics/page_media.php on line 14

    Here are the lines 11-18 in the file page_media.php

    <?php
    		include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    		$feed = fetch_rss('https://pipes.yahoo.com/pipes/pipe.run?_id=e49a6cfb219f8180ded1313941be1808&_render=rss'); // specify feed url
    		$items = array_slice($feed->items, 0, 5); // specify first and last item
    		?>
    
    		<?php if (!empty($items)) : ?>
    		<?php foreach ($items as $item) : ?>
    			<div class="post">
    			<h1 class="post-title"><a>"><?php echo $item['title']; ?></a><?php edit_post_link(' EDIT', '<span>', '</span>'); ?></h1>
    			<?php if (isset($item['description'])) : ?>
    			<div class="entry"><?php echo $item['description']; ?></div>
    			<?php endif; ?>
    			</div>
    
    			<?php endforeach; ?>
    		<?php endif; ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter kasperbs

    (@kasperbs)

    No one in here either?

    Thread Starter kasperbs

    (@kasperbs)

    Anyone know who I could contact about this problem?

    Thread Starter kasperbs

    (@kasperbs)

    This is actually still a problem, if anyone have any input.

    A contact name or email address would be fine, as we can pay for fixing this.

    Well, you could keep it from giving you an error by checking to make sure $feed really is an array before trying to use it. That would look like:
    $items = ( is_array( $feed->items ) ) ? array_slice($feed->items, 0, 5) : NULL;

    If you do that, though, you probably also need to check to make sure the $feed variable is an object, and that it includes the items property. Then, your code would look more like:

    if( is_object( $feed ) && property_exists( $feed, 'item' ) && is_array( $feed->items ) ) {
      $items = array_slice($feed->items, 0, 5);
    }

    Of course, by running these checks, that means that, instead of seeing the error, you’ll just see no output. You could add in some extra code that outputs something like: “There was an error displaying this information. Please try reloading the page.” if $items ends up being empty.

    That doesn’t solve the larger problem, though, which is that your Pipes feed doesn’t seem to be sending the right information on the first try.

    Thread Starter kasperbs

    (@kasperbs)

    Woow thanks. Patience paid off. I will implement it now.

    Thread Starter kasperbs

    (@kasperbs)

    It now looks like this.

    <?php
    		include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    		$feed = fetch_rss('https://pipes.yahoo.com/pipes/pipe.run?_id=e49a6cfb219f8180ded1313941be1808&_render=rss'); // specify feed url
    		if( is_object( $feed ) && property_exists( $feed, 'item' ) && is_array( $feed->items ) ) {
      		$items = array_slice($feed->items, 0, 5); } // specify first and last item
    		?>
    
    		<?php if (!empty($items)) : ?>
    		<?php foreach ($items as $item) : ?>
    			<div class="post">
    			<h1 class="post-title"><a href="<?php echo $item['link']; ?>"><?php echo $item['title']; ?></a><?php edit_post_link(' EDIT', '<span>', '</span>'); ?></h1>
    			<?php if (isset($item['description'])) : ?>
    			<div class="entry"><?php echo $item['description']; ?></div>
    			<?php endif; ?>
    			</div>
    
    		<?php endforeach; ?>
    		<?php endif; ?>
    		<?php if (empty($items)) : ?>
    		<p><em>Der blev ikke fundet nogen aktuelle nyheder p? de s?gte medier.</em></p>
    		<?php endif; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Problem with RSS feeds. array_alice error’ is closed to new replies.