Hi,
I'm just having a small issue with a theme. Some users are seeing the following error when trying to use this flickr widget. I know where the problem is (see below) but not sure how to fix it.
"Warning: Invalid argument supplied for foreach() in /home/www/hnfmedia.com/test/wp-content/themes/FreshStart/functions.php on line 468." Line 468 is the "foreach ($photos" line.
//Get flickr media and display based on user id
function getFlickrPhotos($id, $limit=9) {
require_once("flickr/phpFlickr.php");
$f = new phpFlickr("c1bc021fa23ba9a103aa88743dd9ad33");
$photos = $f->people_getPublicPhotos($id, NULL, NULL, 8);
$return.='<ul class="flickrPhotos">';
foreach ($photos['photos']['photo'] as $photo) {
$return.='<li><a rel="prettyPhoto" href="' . $f->buildPhotoURL($photo, 'medium') . '" title="' . $photo['title'] . '"><img src="' . $f->buildPhotoURL($photo, 'square') . '" alt="' . $photo['title'] . '" title="' . $photo['title'] . '" /></a></li>';
}
echo $return.='</ul>';
echo "<div style='clear:both;'> </div><a style='font-size:11px;' href='http://flickr.com/" .get_option('flickr_name'). "'>See More Photos on Flickr »</a>";
}
add_filter( 'wp_get_attachment_link', 'gallery_prettyPhoto');
function gallery_prettyPhoto ($content) {
// add checks if you want to add prettyPhoto on certain places (archives etc).
return str_replace("<a", "<a rel='prettyPhoto'", $content);
}
Michael Fields answers:
I would suggest the following modifications to the code you posted:
function getFlickrPhotos( $id, $limit=9 ) {
require_once( "flickr/phpFlickr.php" );
$f = new phpFlickr( "c1bc021fa23ba9a103aa88743dd9ad33" );
$photos = $f->people_getPublicPhotos( $id, NULL, NULL, 8 );
$photo_list = '';
if ( isset( $photos['photos']['photo'] ) ) {
foreach ( (array) $photos['photos']['photo'] as $photo ) {
$photo_list .= '<li><a rel="prettyPhoto" href="' . esc_url( $f->buildPhotoURL( $photo, 'medium' ) ) . '" title="' . esc_attr( $photo['title'] ) . '"><img src="' . esc_url( $f->buildPhotoURL( $photo, 'square' ) ) . '" alt="' . esc_attr( $photo['title'] ) . '" title="' . esc_attr( $photo['title'] ) . '" /></a></li>';
}
}
if ( ! empty( $photo_list ) ) {
if ( isset( $return ) ) {
print $return;
}
print '<ul class="flickrPhotos">' . $photo_list . '</ul>';
print "<div style='clear:both;'> </div><a style='font-size:11px;' href='http://flickr.com/" . get_option('flickr_name') . "'>See More Photos on Flickr »</a>";
}
}
add_filter( 'wp_get_attachment_link', 'gallery_prettyPhoto' );
To fix only the issue you asked about, the following should be sufficient:
foreach ( (array) $photos['photos']['photo'] as $photo ) {
It's good practice to always cast variables as arrays when passing them to foreach().
Rashad Aliyev answers:
Suggestion My Pretty Flickr Widget code:
P.S: Also you can select from options for photo random or latest.
<?php
// =============================== Flickr widget ======================================
class flickrWidget extends WP_Widget {
function flickrWidget() {
//Constructor
$widget_ops = array('classname' => 'widget Flickr Photos ', 'description' => 'Flickr Photos' );
$this->WP_Widget('widget_flickrwidget', 'CVR → Flickr Photos Widget', $widget_ops);
}
function widget($args, $instance) {
// prints the widget
extract($args, EXTR_SKIP);
echo $before_widget;
$id = empty($instance['id']) ? ' ' : apply_filters('widget_id', $instance['id']);
$number = empty($instance['number']) ? ' ' : apply_filters('widget_number', $instance['number']);
$showing = empty($instance['showing']) ? ' ' : apply_filters('widget_showing', $instance['showing']);
?>
<h3><span>Photo Gallery</span></h3>
<div class="flickr clearfix">
<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=<?php echo $number; ?>&display=<?php echo $showing; ?>&size=s&layout=x&source=user&user=<?php echo $id; ?>"></script>
</div>
</div>
<?php
}
function update($new_instance, $old_instance) {
//save the widget
$instance = $old_instance;
$instance['id'] = strip_tags($new_instance['id']);
$instance['number'] = strip_tags($new_instance['number']);
$instance['showing'] = strip_tags($new_instance['showing']);
return $instance;
}
function form($instance) {
//widgetform in backend
$instance = wp_parse_args( (array) $instance, array('title' => '', 'id' => '', 'number' => '' , 'showing' => '') );
$id = strip_tags($instance['id']);
$number = strip_tags($instance['number']);
$showing = strip_tags($instance['showing']);
?>
<p>
<label for="<?php echo $this->get_field_id('id'); ?>">Flickr ID (<a target='_blank' href="http://www.idgettr.com">idGettr</a>):
<input class="widefat" id="<?php echo $this->get_field_id('id'); ?>" name="<?php echo $this->get_field_name('id'); ?>" type="text" value="<?php echo attribute_escape($id); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>">Number of photos:
<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo attribute_escape($number); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('showing'); ?>">Showing Method:
<select size="1" name="<?php echo $this->get_field_name('showing'); ?>">
<option value="random"<?php if(attribute_escape($showing) =='random'){echo 'selected';}?>>Random Photo</option>
<option value="latest"<?php if(attribute_escape($showing) =='latest'){echo 'selected';}?>>Latest Photo</option>
</select>
</label>
</p>
<?php
}
}
register_widget('flickrWidget');
?>