Shorten PHP code with While Loop - php

I'm trying to simplify the code below, but I'm still struggling to understand it. Code A works fine, it outputs a list of images, but Code B just shows a single image
Code A
<?php
$html_form = '<label>
<img src="%1$s" width="%2$s" height="%3$s" alt="%4$s" />
<input type="checkbox" name="%5$s[]" value="%6$s" %7$s/>
</label>';
$html_td = '';
$html = '';
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$select = '';
$thumb = wp_get_attachment_image_src($id, array(30, 30));
if (is_array($instance['thumbs']) && in_array($id, $instance['thumbs'])) {
$select = 'checked="checked"';
}
$html .= sprintf($html_td);
$html_td = '';
$html_td .= sprintf($html_form, $thumb[0], $thumb[1], $thumb[2], get_the_title(), $this->get_field_name('thumbs'), $id, $select);
}
$html_form = '
<p>
%s
</p>';
printf($html_form, $html);
?>
My Attempt :
Code B
<?php
while ($query->have_posts()) {
$query->the_post();
$id = get_the_ID();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;
}
?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>

As mentioned in the comments it's because you're only echoing out one image.
You could do something like this:
<?php while($query->have_posts()):
$query->the_post();
$id = get_the_id();
$thumb = wp_get_attachment_image_src($id, array(30, 30));
$thumbs = is_array($instance['thumbs']) ? (bool) in_array($id, $instance['thumbs']) : true;?>
<p>
<label>
<img src="<?php echo $thumb[0] ?>" width="<?php echo $thumb[1] ?>" height="<?php echo $thumb[2] ?>" alt="<?php echo get_the_title() ?>" />
<input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name('thumbs'); ?>" value="<?php echo $id ?>" <?php checked($thumbs); ?>/>
</label>
</p>
<?php endwhile;
wp_reset_postdata();
In this case the HTML is inside the while and an image will echo out for each iteration. Also, make sure you have the wp_reset_postdata() or else you may (or may not) encounter bugs in the future.

Related

Class WP_Widget not found

This is from an unsupported WordPress plugin that I'm trying to revive. I'm getting an error in the following code which states
Fatal Error class WP_Widget not found on Line 7
It works with PHP 5.3.3 but not on 5.6 or any version of PHP 7. Any suggestions would be appreciated.
<?php
/**
*
* Widget Class
*/
class Twitter_Like_Box_Widget extends WP_Widget
{
var $_options;
function __construct( ) {
global $tlb;
$this->wpb_prefix = $tlb->get_domain();
$widget_ops = array( 'classname' => 'tlb_widget', 'description' => ' Display your Twitter followers along with a follow me button' ); // Widget Settings
$control_ops = array( 'id_base' => 'tlb_widget' ); // Widget Control Settings
parent::__construct( 'tlb_widget', 'Twitter Like Box', $widget_ops, $control_ops );
$this->_options = $tlb->getOptions();
}
//Function to init the widget values and call the display widget function
function widget($args,$instance)
{
$title = apply_filters('widget_title', $instance['title']); // the widget title
$username = $instance['username']; // the widget title
$total_number = $instance['total_number']; // the number of followers to show
$show_followers = $instance['show_followers']; // show followers or users i follow
$link_followers = $instance['link_followers']; // link followers to profile
$width = $instance['width']; // link followers to profile
$widget = array ( 'username' => $username ,'total' => $total_number , 'show_followers' => $show_followers ,'link_followers'=> $link_followers, 'width' => $width, 'options' => $this->_options);
echo $args['before_widget'];
if ( $title )
echo $args['before_title'] . $title . $args['after_title'];
$this->display_widget($widget);
echo $args['after_widget'];
}
//function that display the widget form
function form($instance)
{
global $tlb;
$defaults = array( 'total_number' => 10, 'show_followers' => 'followers','link_followers'=> 'on','title' => 'My Followers', 'username' => 'chifliiiii');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<?php if( $tlb->error != '' && defined('DOING_AJAX')):?>
<div class="error">
<p><?php echo sprintf(__('Check you OAuth settings, there is a problem with the connection.',$this->wpb_prefix),admin_url('options-general.php?page=twitter-like-box-reloaded'));?></p>
</div>
<?php endif;?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:',$this->wpb_prefix);?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>'" type="text" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username (without #):',$this->wpb_prefix);?></label>
<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>'" type="text" value="<?php echo $instance['username']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('show_thumbs'); ?>"><?php _e('Show Followers or people you follow?',$this->wpb_prefix);?></label>
</p>
<ul>
<li>
<input type="radio" class="radio" <?php checked( $instance['show_followers'], 'followers' ); ?> id="<?php echo $this->get_field_id('show_followers'); ?>" name="<?php echo $this->get_field_name('show_followers'); ?>" value="followers"/> <?php _e('Followers',$this->wpb_prefix);?>
</li>
<li>
<input type="radio" class="radio" <?php checked( $instance['show_followers'], 'nofollowers' ); ?> id="<?php echo $this->get_field_id('show_followers'); ?>" name="<?php echo $this->get_field_name('show_followers'); ?>" value="nofollowers" /> <?php _e('People I follow',$this->wpb_prefix);?>
</li>
</ul>
<p>
<label for="<?php echo $this->get_field_id('total_number'); ?>"><?php _e('How many you want to show?',$this->wpb_prefix); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('total_number'); ?>" name="<?php echo $this->get_field_name('total_number'); ?>" type="text" value="<?php echo $instance['total_number']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('link_followers'); ?>"><?php _e('Link followers to their profiles?',$this->wpb_prefix); ?></label>
<input type="checkbox" class="checkbox" <?php checked( $instance['link_followers'], 'on' ); ?> id="<?php echo $this->get_field_id('link_followers'); ?>" name="<?php echo $this->get_field_name('link_followers'); ?>" value="on" />
</p>
<p>
<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Widget width:',$this->wpb_prefix); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width') ; ?>'" type="text" value="<?php echo isset($instance['width']) ? $instance['width'] : '100%'; ?>" />
</p>
<p>
<?php _e('More settings',$this->wpb_prefix);?>
</p>
<?php
}
//function that save the widget
function update($new_instance, $old_instance)
{
$instance['title'] = strip_tags($new_instance['title']);
$instance['username'] = strip_tags( $new_instance['username'] )== '' ? 'chifliiiii' : strip_tags( $new_instance['username'] );
$instance['total_number'] = strip_tags($new_instance['total_number']);
$instance['show_followers'] = $new_instance['show_followers'];
$instance['link_followers'] = $new_instance['link_followers'];
$instance['width'] = $new_instance['width'];
//Delete transient in case exist
$key = 'tlb_widgets_' . $instance['username'];
delete_transient($key);
return $instance;
}
//Finally thevfunction that create the widget
static function get_tlb_widget($widget)
{
global $tlb,$you;
$wpb_prefix = $tlb->get_domain();
$twitter = self::fetch_twitter_followers($widget);
ob_start();
if( !empty($you['error']) && '32' == $you['code']) {
echo $you['error'];
}
else
{
?>
<style type="text/css">
<?php echo $widget['options']['custom_css'];?>
</style>
<div id="tlb_container" style="width: <?php echo isset($widget['width']) ? $widget['width'] : 'auto';?>">
<?php if(isset($twitter['error']) ) :?>
<?php echo $twitter['error'];?>
<?php else : ?>
<div>
<div id="tlb_profile_img">
<a target="_blank" href="http://twitter.com/<?php echo $widget['username'];?>">
<img src="<?php echo $twitter['profile_image_url'];?>" width="44" height="44" align="left" alt="<?php echo $widget['username'];?>">
</a>
</div>
<div id="tlb_name">
<a target="_blank" href="http://twitter.com/<?php echo $widget['username'];?>">
<?php echo $widget['username'];?><span> <?php _e('on Twitter',$wpb_prefix);?></span>
</a>
</div>
<div id="tlb_follow">
<?php _e('Follow #',$wpb_prefix);?><?php echo $widget['username'];?>
</div>
</div><br>
<div style="padding:0; color:#637746;">
<div id="tlb_follow_total">
<?php
if ( $widget['show_followers'] == 'followers')
{
echo $twitter['followers_count'].' '.__('people follow',$wpb_prefix).' <strong>'. $widget['username'].'</strong>';
}
else
{
echo __('You follow ',$wpb_prefix). $twitter['friends_count'].__(' users',$wpb_prefix);
}
?>
</div>
<?php for($i=0; $i < $widget['total']; $i++) :?>
<span class="tlb_user_item">
<?php if($widget['link_followers'] == 'on' ): ?>
<a target="_blank" href="http://twitter.com/<?php echo $twitter['followers'][$i]['screen_name'];?>" title="<?php echo $twitter['followers'][$i]['screen_name'];?>" rel="nofollow">
<?php endif;?>
<img src="<?php echo $twitter['followers'][$i]['profile_image_url'];?>" width="48" height="48" alt="<?php echo $twitter['followers'][$i]['screen_name'];?>">
<span><?php echo substr($twitter['followers'][$i]['screen_name'], 0, 8);?></span>
<?php if($widget['link_followers'] == 'on' ): ?>
</a>
<?php endif;?>
</span>
<?php endfor;?>
<br style="clear:both">
</div>
<?php if ( $widget['options']['credits'] == 'true' ) echo '<div style="font-size:9px;text-align:right;">Widget By Timersys</div>';?>
<?php endif;//twitter error ?>
</div>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>
<?php
}
$widget_code = ob_get_contents();
ob_end_clean();
return $widget_code;
}
/**
* Display widget
*/
static function display_widget($options){
echo Twitter_Like_Box_Widget::get_tlb_widget($options);
}
static function fetch_twitter_followers($options)
{
global $tlb,$you;
$cache_time = $tlb->_options['cache-time'];
$id = isset($options['id']) ? $options['id'] : 'widgets';
$key = 'tlb_'.$id.'_' . $options['username'];
// Let's see if we have a cached version
$followers = get_transient($key);
if ($followers !== false)
return $followers;
else
{
$tlb->connect();
$response = $tlb->connection->get("users/lookup", array('screen_name' => $options['username']));
if (Twitter_Like_Box_Widget::is_twitter_error($response))
{
// In case Twitter is down we return the last successful count
return get_option($key);
}
else
{
$json = $response;
#$you['name'] = $json[0]->name;
#$you['screen_name'] = $json[0]->screen_name;
#$you['followers_count'] = $json[0]->followers_count;
#$you['profile_image_url'] = $json[0]->profile_image_url;
#$you['friends_count'] = $json[0]->friends_count;
if ( $options['show_followers'] == 'followers' )
{
$fans = $tlb->connection->get('followers/ids',array('screen_name' => $options['username']));
}
else
{
$fans = $tlb->connection->get('friends/ids',array('screen_name' => $options['username']));
}
if (!Twitter_Like_Box_Widget::is_twitter_error($fans))
{
if ($options['total'] > 90 )
{
$fans_ids = array_chunk($fans->ids, 90);
$fans = array();
foreach ( $fans_ids as $ids_a )
{
$fans_ids = (string)implode( ',', $ids_a );
#$result = $tlb->connection->get('users/lookup',array('user_id' => $fans_ids ));
#$fans = array_merge($fans , $result );
}
}
else
{
$fans_ids = (string)implode( ',', array_slice($fans->ids, 0, $options['total']) );
#$fans = $tlb->connection->get('users/lookup',array('user_id' =>$fans_ids));
}
}
if( !Twitter_Like_Box_Widget::is_twitter_error($fans) && isset($fans[0]->screen_name) )
{
$followers = array();
for($i=0; $i < $options['total']; $i++)
{
$followers[$i]['screen_name'] = (string)$fans[$i]->screen_name;
$followers[$i]['profile_image_url'] = (string)$fans[$i]->profile_image_url;
}
$you['followers'] = $followers;
// Store the result in a transient, expires after 1 hour
// Also store it as the last successful using update_option
set_transient($key, $you, 60*60* $cache_time);
update_option($key, $you);
}
return $you;
}
}
}
static function is_twitter_error($response){
global $you,$tlb;
if(is_object($response) && isset($response->errors) )
{
$you['error'] = 'Error code: '. $response->errors[0]->code .'<br>Error message: '.$response->errors[0]->message;
$you['code'] = $response->errors[0]->code;
$tlb->log_error($you['error']);
return true;
}
if(is_object($response) && isset($response->ids) && empty($response->ids))
{
$you['error'] = '<br>Error message: You got no users to show. check if you have followers';
$tlb->log_error($you['error']);
return true;
}
return false;
}
} //end of class
Wordpress have ABSPATH variable defined in wp-config.php file, which contains the project's root path. Now we can include any file of wordpress codebase. eg.
require_once ABSPATH.'wp-includes/class-wp-widget.php';
I don't know the reason for the error, propably something with autoloading, but you can solve it if you include the file for the WP_Widget class in the first line.
require_once get_home_path()."wp-includes/class-wp-widget.php";

How to loop a php code?

I wanted to do the DRY approach in my code but I'm having a hard time figuring it out. And also, I want to hide the entire code if there's no image_1. Hope you could help me do the trick.
Here's the code
<div class="col-md-4">
<?php
$image = get_field('image_1');
if(get_field('image_1'))
{
echo '<a href="' . get_field('image_link_1') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_2');
if(get_field('image_2'))
{
echo '<a href="' . get_field('image_link_2') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<div class="col-md-4">
<?php
$image = get_field('image_3');
if(get_field('image_3'))
{
echo '<a href="' . get_field('image_link_3') . '">';?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
You should put differences to arrays and then wrap everything into for loop:
<?php
$images = array('image_1', 'image_2', 'image_3');
$links = array('image_link_1', 'image_link_2', 'image_link_3');
for($i=0; $i<3; $i++){
?>
<div class="col-md-4">
<?php
$image = get_field($images[$i]);
if(get_field($images[$i])){
echo '<a href="' . get_field($links[$i]) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
}
?>
</div>
<?php
}
?>
Just a hint...:
<?php
for ($i = 0; $i < 3; $i++) {
echo "<div class='col-md-4'>" . "\n";
$image = get_field("image_" . ($i + 1));
...
echo "</div>" . "\n";
}
?>
Something along these lines should get you started if I'm understanding you correctly:
<?php for ($q = 1; $q <= 3; $q++) {
$image_loop = 'image_' . $q;
echo '<div class="col-md-4">';
if ($image = get_field($image_loop)) {
echo '<a href="' . get_field('image_link_' . $q) . '">';
?>
<img src="<?php echo $image['url']; ?>" title="<?php echo $image['title']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php echo '</a>';
} else {
echo '<img src="http://localhost/image.png">';
} ?>
</div>
<?php } // end loop ?>
The other suggestions here will work as well but here's what I would do:
First arrange the images in an associative array with keys being the image name and values being the image link and then iterate via a foreach loop.
I generally try to not echo HTML unless strictly necessary.
<?php
$array = [
"image_1" => "image_link_1",
"image_2" => "image_link_2",
"image_3" => "image_link_3",
"image_4" => "image_link_4"
];
foreach ($array as $name => $link):
$image = get_field($name);
if ($image): ?>
<div class="col-md-4">
<a href="<?=get_field($link)?>">
<img src="<?= $image['url']; ?>" title="<?= $image['title']; ?>" alt="<?= $image['alt']; ?>" />
</a>
<?php else: ?>
<img src="http://localhost/image.png">
<?php endif; ?>
</div>
<?php endforeach; ?>

Passing multiple arrays to for each loop..i think

I love jQuery and dont really understand php.
I am making a slider with albums. So far so good except I am now stuck trying to add two arrays into one for each loop. At least I think that is the best solution.
In my code you can see I have achieved what I need by hard coding 6 images for the 6 custom fields in the custom post type (I am using wordpress). The problem is that if there are not six images then the slider show a blank image (as it exists but doesnt have a src). I tried removing the element with jquery but that was no good. Here is the code I have so far, perhaps there is something i am missing, I just cant seem to get the logic quite right.
<?php
$args = array(
'post_type' => 'albums_gallery',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$album_name = get_the_ID();
$image_1 = get_field('image_1');
$image_1_url = $image_1['url'];
$image_1_caption = get_field('image_1_caption');
$image_2 = get_field('image_2');
$image_2_url = $image_2['url'];
$image_2_caption = get_field('image_2_caption');
$image_3 = get_field('image_3');
$image_3_url = $image_3['url'];
$image_3_caption = get_field('image_3_caption');
$image_4 = get_field('image_4');
$image_4_url = $image_4['url'];
$image_4_caption = get_field('image_4_caption');
$image_5 = get_field('image_5');
$image_5_url = $image_5['url'];
$image_5_caption = get_field('image_5_caption');
$image_6 = get_field('image_6');
$image_6_url = $image_6['url'];
$image_6_caption = get_field('image_6_caption');
?>
<div class="album album_<?php echo $album_name ?>">
<div class="slider-wrapper theme-default">
<div class="slider" class="nivoSlider">
<!--<img src="<?php echo $image_1['url']; ?>" alt="<?php echo $image_1['alt']; ?>" title="<?php echo $image_1_caption; ?>" />
<img src="<?php echo $image_2['url']; ?>" alt="<?php echo $image_2['alt']; ?>" title="<?php echo $image_2_caption; ?>" />
<img src="<?php echo $image_3['url']; ?>" alt="<?php echo $image_3['alt']; ?>" title="<?php echo $image_3_caption; ?>" />
<img src="<?php echo $image_4['url']; ?>" alt="<?php echo $image_4['alt']; ?>" title="<?php echo $image_4_caption; ?>" />
<img src="<?php echo $image_5['url']; ?>" alt="<?php echo $image_5['alt']; ?>" title="<?php echo $image_5_caption; ?>" />
<img src="<?php echo $image_6['url']; ?>" alt="<?php echo $image_6['alt']; ?>" title="<?php echo $image_6_caption; ?>" />-->
<?php
$images = array("$image_1_url","$image_2_url","$image_3_url","$image_4_url", "$image_5_url", "$image_6_url");
foreach ($images as $image) {
if ($image != "") {
echo "<img src='";
echo $image;
echo "' ";
echo "title='caption'";
echo "/>";
}
};
?>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
I need to add the image caption, and probably a link into the for each and if statements, little out of my depth being a designer.
Thanks for any help.
This is all you need
<div class = "slider-wrapper theme-default">
<div class = "slider" class = "nivoSlider">
<?php
$images = Array();
for($i = 1; $i <= 6; $i++) {
$image = get_field("image_{$i}");
if(!$image || !$image['url']) {
break;
}
$caption = get_field("image_{$i}_caption");
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" title="<?php echo $caption; ?>" />
<?php
}
?>

Output reverse count foreach loop php

I have the follow foreach loop:
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=1;
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count++; }; ?>
I need help getting the count to reverse. The output I am looking for is as follows:
<div id="slide4"></div>
<div id="slide3"></div>
<div id="slide2"></div>
<div id="slide1"></div>
Any help with this would be great.
Change $count=1; to $count = sizeof($arrayreversed);
and $count++; to $count--;
First of all get the amount of keys in the array and assign it as $count, like this:
$count = count($arrayreversed);
Then replace the $count++ with $count-- and you're all set.
How about using the size of the array as your initial count and then subtracting 1 each iteration?
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=count($arrayreversed);
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count--; }; ?>

foreach is closing after first value then displaying the remaining values

I wrote a wordpress widget that lists the categories of the blog in a tag. Initially the code I wrote works and you can select a value and when you click save the widget displays the correct information on the front end. However after you hit save when the array is reloaded it looks like this:
<select name="widget-cheer_recent_posts[7][cat]" id="widget-cheer_recent_posts-7-cat" class="widefat"></select>
<option value="72" id="Elite">Elite</option>
</p>
<option value="96" id="Featured">Featured</option>
<option value="70" id="Junior Varsity">Junior Varsity</option>
<option value="105" id="JV">JV</option>
<option value="67" id="Mixes">Mixes</option>
<option value="97" id="promo">promo</option>
<option value="99" id="Samples">Samples</option>
<option value="1" id="Uncategorized">Uncategorized</option>
<option value="71" id="Varsity">Varsity</option>
I'm not sure how this is happening as the dropdown loads correctly the first time and it is only upon the save and AJAX reload in wordpress that it fails. Here is the code for the widget:
define ('CHEER_PLUGIN_DIR', dirname( plugin_basename( __FILE__ ) ));
define ('CHEER_PLUGIN_URL', plugins_url() . '/' . CHEER_PLUGIN_DIR);
class cheer_recent_posts extends WP_Widget {
/** constructor */
function cheer_recent_posts() {
parent::WP_Widget(false, $name = 'Cheer Samples');
}
/** #see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
global $posttypes;
global $cats;
$title = apply_filters('widget_title', $instance['title']);
$cat = $instance['cat'];
$number = apply_filters('widget_title', $instance['number']);
$offset = apply_filters('widget_title', $instance['offset']);
$thumbnail_size = apply_filters('widget_title', $instance['thumbnail_size']);
$thumbnail = $instance['thumbnail'];
$posttype = $instance['posttype'];
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<?php
global $post;
$tmp_post = $post;
// get the category IDs and place them in an array
$args = 'numberposts=' . $number . '&offset=' . $offset . '&post_type=' . $posttype . '&cat=' . $cat;
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<div id="cs-background" style="border: 5px solid white; background:url(<?php echo (CHEER_PLUGIN_URL); ?>/timthumb/timthumb.php?src=<?php echo get_post_meta($post->ID, 'nitro_post_image', true) ?>&w=270&h=157&zc=1) no-repeat;">
<div class="cs-audio">
<audio style="width:270px; margin:115px 0 0 0;" controls="controls">
<source src="<?php echo get_post_meta($post->ID, 'song-sample', true) ?>" type="audio/mp3" />
<object type="application/x-shockwave-flash" data="<?php echo (CHEER_PLUGIN_URL); ?>/dewplayer/dewplayer-mini.swf" width="266" height="20" id="dewplayer" name="dewplayer">
<param name="wmode" value="transparent" />
<param name="movie" value="dewplayer-mini.swf" />
<param name="flashvars" value="mp3=<?php echo get_post_meta($post->ID, 'song-sample', true) ?>" />
</object>
</audio>
</div>
</div>
<div>
<?php the_title( '<h2 id="mix-title" class="post-title entry-title">', '</h2>' ); ?>
<!-- <h2 id="cs-title" class="post-title entry-title">
<?php if($thumbnail == true) { ?>
<?php the_post_thumbnail(array($thumbnail_size));?>
<?php } ?>
<?php the_title(); ?>
</h2>
<?php the_content(); ?> -->
</div>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
<?php echo $after_widget; ?>
<?php
}
/** #see WP_Widget::update */
function update($new_instance, $old_instance) {
global $posttypes;
global $cats;
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['cat'] = $new_instance['cat'];
$instance['number'] = strip_tags($new_instance['number']);
$instance['offset'] = strip_tags($new_instance['offset']);
$instance['thumbnail_size'] = strip_tags($new_instance['thumbnail_size']);
$instance['thumbnail'] = $new_instance['thumbnail'];
$instance['posttype'] = $new_instance['posttype'];
return $instance;
}
/** #see WP_Widget::form */
function form($instance) {
$posttypes = get_post_types('', 'objects');
$cats = get_categories('hide_empty=0');
$title = esc_attr($instance['title']);
$cat = esc_attr($instance['cat']);
$number = esc_attr($instance['number']);
$offset = esc_attr($instance['offset']);
$thumbnail_size = esc_attr($instance['thumbnail_size']);
$thumbnail = esc_attr($instance['thumbnail']);
$posttype = esc_attr($instance['posttype']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e('Category you want to use'); ?></label>
<select name="<?php echo $this->get_field_name('cat'); ?>" id="<?php echo $this->get_field_id('cat'); ?>" class="widefat" />
<?php
foreach ($cats as $catsoption) {
echo '<option value="' . $catsoption->cat_ID . '" id="' . $catsoption->name . '"', $cat == $catsoption->cat_ID ? ' selected="selected"' : '', '>', $catsoption->name, '</option>';
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Offset (the number of posts to skip):'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="text" value="<?php echo $offset; ?>" />
</p>
<p>
<input id="<?php echo $this->get_field_id('thumbnail'); ?>" name="<?php echo $this->get_field_name('thumbnail'); ?>" type="checkbox" value="1" <?php checked( '1', $thumbnail ); ?>/>
<label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e('Display thumbnails?'); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('thumbnail_size'); ?>"><?php _e('Size of the thumbnails, e.g. <em>80</em> = 80px x 80px'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('thumbnail_size'); ?>" name="<?php echo $this->get_field_name('thumbnail_size'); ?>" type="text" value="<?php echo $thumbnail_size; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('posttype'); ?>"><?php _e('Choose the Post Type to display'); ?></label>
<select name="<?php echo $this->get_field_name('posttype'); ?>" id="<?php echo $this->get_field_id('posttype'); ?>" class="widefat">
<?php
foreach ($posttypes as $option) {
echo '<option value="' . $option->name . '" id="' . $option->name . '"', $posttype == $option->name ? ' selected="selected"' : '', '>', $option->name, '</option>';
}
?>
</select>
</p>
<?php
}
} // class utopian_recent_posts
// register Recent Posts widget
add_action('widgets_init', create_function('', 'return register_widget("cheer_recent_posts");'));

Categories