开发者

Warning: array_slice() expects parameter 1 to be array, null given in [closed]

Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 3 years ago.

Improve this question

I'm using the following code in my WordPress site to display the latest Youtube video from my account using rss:

<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_rss('http://gdata.youtube.com/feeds/api/users/UrbanGAME/uploads'); // specify feed url
$items = array_slice($feed->items, 0, 1); // specify first and last item
?>

<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>

<object width="96%"><param name="movie" value="<?php echo str_replace("watch?v=","v/", $ite开发者_JAVA百科m['link']); ?>&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="96%"></embed></object>

<?php endforeach; ?>
<?php endif; ?>

It works. However, some of the times I get this error when it does not work:

 Warning: array_slice() expects parameter 1 to be array, null given in /wp-content/themes/theme1/header.php  on line 173. 

Also I'm using the above code twice, for 2 spots on the site for 2 different accounts. One is in the my header.php file and the other in my footer.php file. When 1 works, the other does not, and of course I get an error/warning for the other one as well, just referring to that file and a different line. I have tried messing with the code and still nothing. Could the problem be with something in my code?

Additional info:

php: 5.3.2

host: crazy domains (http://www.crazydomains.com.au/)

apache: 2.2.15


The reason it may not work is because $feed->items may not be an array(based on the fetch_rss call).

To check if something is an array, you can do:

is_array($feed->items); // returns true or false

If it is an array, you can then call array_slice()

The better solution however, is to check the $feed variable to make sure it returned what you are looking for before processing.


Looks like $feed->items may or may not be an array. That all depends on how $feed gets assigned, which depends on whats happening inside fetch_rss

Although tis somewhat oblique to your problem, it appears you have some superfluous code. You do the slice to get the first element the array alone, and then you iterate on a list with just that one element? Instead you can simply perform whatever you like on that first element.

<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_rss('http://gdata.youtube.com/feeds/api/users/UrbanGAME/uploads'); // specify feed url
$firstItem = exists($feed->items[0]) ? $feed->items[0] : null;
?>

<?php if ($firstItem) : ?>
<object width="96%"><param name="movie" value="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="<?php echo str_replace("watch?v=","v/", $firstItem['link']); ?>&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="96%"></embed></object>
<?php endif; ?>


I've had this same problem with an RSS feed related to a theme I'm using. On the admin side, the code is supposed to display more themes by the same artist, however I'm getting the same array_slice error, because $feed->items is null.

The link to the RSS feed is working properly, and there is information at the address. So, I started looking around, and I found that the problem is with the RSS, not the code (well, the original problem. It's still a problem that that error is displayed). I pointed the fetch_rss() command at a different feed, and it works fine.

Until the artist adjusts his feed to work with the command, there's nothing I can personally do. But if you're having trouble getting this command to work with your own feeds, try making sure the feeds are in the required format for this Wordpress command.

Sorry to post on an old question, but there are no direct answers elsewhere on the web.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜