foreach is closing after first value then displaying the remaining values - php

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");'));

Related

is this correct to fix Use of deprecated PHP4 style class constructor is not supported since PHP 7?

no idea what i'm doing , but in reading some other similar questions , i wanted to know if this is correct before i make the change
I am getting the error
Use of deprecated PHP4 style class constructor is not supported since PHP 7
when i run a compatibility test on my WP Blog
It references these 2 lines
function boc_latest() {
$widget_ops = array('description' => 'Aqua Latest Posts');
$this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
}
function contact_info_widget()
{
$widget_ops = array('classname' => 'contact_info', 'description' => '');
$this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
}
Would i simply change the functions as such ?
function __construct() {
$widget_ops = array('description' => 'Aqua Latest Posts');
$this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
}
function __construct()
{
$widget_ops = array('classname' => 'contact_info', 'description' => '');
$this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
}
Here is full php file
function boc_load_widgets() {
register_widget('boc_latest');
register_widget('contact_info_widget');
}
class boc_latest extends WP_Widget {
function boc_latest() {
$widget_ops = array('description' => 'Aqua Latest Posts');
$this->WP_Widget('boc_latest', 'Aqua Latest Posts', $widget_ops);
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : '<span>'.apply_filters('widget_title', $instance['title']).'</span>';
$count = $instance['count'];
echo removeSpanFromTitle($before_title) . $title . removeSpanFromTitle($after_title);
wp_reset_query();
rewind_posts();
$recent_posts = new WP_Query(
array(
'posts_per_page' => $count,
'post_status' => 'publish',
'nopaging' => 0,
'post__not_in' => get_option('sticky_posts')
)
);
// Cycle through Posts
if ($recent_posts->have_posts()) :while ($recent_posts->have_posts()) : $recent_posts->the_post();
?>
<div class="boc_latest_post clearfix">
<?php the_post_thumbnail('small-thumb'); ?>
<p class="boc_latest_post_title"><?php the_title(); ?></p>
<p class="date"><?php echo get_the_date();?></p>
</div>
<?php
endwhile;
endif;
wp_reset_query();
rewind_posts();
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'];
return $instance;
}
function form($instance) {
$instance = wp_parse_args((array) $instance, array('title' => ''));
$title = strip_tags($instance['title']);
$count = $instance['count'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Widget Title:
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>">How many posts? (Number):
<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo esc_attr($count); ?>" />
</label>
</p>
<?php
}
}
/**
* Contact Info Widget
*/
class contact_info_widget extends WP_Widget {
function contact_info_widget()
{
$widget_ops = array('classname' => 'contact_info', 'description' => '');
$this->WP_Widget('contact_info-widget', 'Aqua: Contact Info', $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if($title) {
echo $before_title.$title.$after_title;
}
?>
<?php if($instance['phone']): ?>
<div class="icon_phone"><?php echo $instance['phone']; ?></div>
<?php endif; ?>
<?php if($instance['email']): ?>
<div class="icon_mail"><?php echo $instance['email']; ?></div>
<?php endif; ?>
<?php if($instance['address']): ?>
<div class="icon_loc"><?php echo $instance['address']; ?></div>
<?php endif; ?>
<div class="clear h10"></div>
<?php
echo $after_widget;
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['address'] = $new_instance['address'];
$instance['phone'] = $new_instance['phone'];
$instance['fax'] = $new_instance['fax'];
$instance['email'] = $new_instance['email'];
$instance['web'] = $new_instance['web'];
return $instance;
}
function form($instance)
{
$defaults = array('title' => 'Contact Info');
$instance = wp_parse_args((array) $instance, $defaults); ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('phone'); ?>">Phone:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('phone'); ?>" name="<?php echo $this->get_field_name('phone'); ?>" value="<?php echo $instance['phone']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('email'); ?>">Email:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('email'); ?>" name="<?php echo $this->get_field_name('email'); ?>" value="<?php echo $instance['email']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('address'); ?>">Address:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('address'); ?>" name="<?php echo $this->get_field_name('address'); ?>" value="<?php echo $instance['address']; ?>" />
</p>
<?php
}
}
class boc_latest extends WP_Widget {
function boc_latest(){
...
}
}
function boc_latest() Should be renamed function __construct() because the class name is the same as the method (function) name.
class boc_latest extends WP_Widget {
function __construct(){
...
}
}
The same applies universally; If a class contains a function with an identical name (and no __construct exists) then tha function/method should be renamed as a __construct() method for that class.
From the Manual
For backwards compatibility with PHP 3 and 4, if PHP cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.
Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.

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";

Customize content in WooCommerce admin quick order preview

I am new to WordPress and I am using WooCommerce plugin in my project where I need to modify some of template files and some of core files of WooCommerce.
I have created child theme and created woocommerce folder inside my child theme and hence I can create/modify template files there which customizations won't be lost during WooCommerce plugin updates. But as I have read on some articles, modifying core files of WooCommerce is not recommended.
I have customized in WooCommerce plugin this core file includes\admin\meta-boxes\views\html-order-item and I am a little worried about future plugin updates (Are my custom changes will stay preserved or not). I have tried to copy this file in my child theme inside the woocommerce folder, but it's not seems to override the original one, so it has no effect.
In this "html-order-item.php" file, I have just added a small piece of code which has a download button to downloads product's original image.
How can I achieve this using a "clean recommended way ( using hooks/filters or templates)?
Please suggest any way to achieve it.
Here is my code snippet (my customization starts with /* Custom code added by Amol */ ):
<?php
/**
* Shows an order item
*
* #var object $item The item being displayed
* #var int $item_id The id of the item being displayed
*/
if (!defined('ABSPATH')) {
exit;
}
$product_link = $_product ? admin_url('post.php?post=' . absint($_product->id) . '&action=edit') : '';
$thumbnail = $_product ? apply_filters('woocommerce_admin_order_item_thumbnail', $_product->get_image('thumbnail', array('title' => ''), false), $item_id, $item) : '';
$tax_data = empty($legacy_order) && wc_tax_enabled() ? maybe_unserialize(isset($item['line_tax_data']) ? $item['line_tax_data'] : '' ) : false;
$item_total = ( isset($item['line_total']) ) ? esc_attr(wc_format_localized_price($item['line_total'])) : '';
$item_subtotal = ( isset($item['line_subtotal']) ) ? esc_attr(wc_format_localized_price($item['line_subtotal'])) : '';
<tr class="item <?php echo apply_filters('woocommerce_admin_html_order_item_class', (!empty($class) ? $class : ''), $item, $order); ?>" data-order_item_id="<?php echo $item_id; ?>">
<td class="thumb">
<?php
echo '<div class="wc-order-item-thumbnail">' . wp_kses_post($thumbnail) . '</div>';
?>
</td>
<td class="name" data-sort-value="<?php echo esc_attr($item['name']); ?>">
<?php
echo $product_link ? '' . esc_html($item['name']) . '' : '<div class="class="wc-order-item-name"">' . esc_html($item['name']) . '</div>';
if ($_product && $_product->get_sku()) {
echo '<div class="wc-order-item-sku"><strong>' . __('SKU:', 'woocommerce') . '</strong> ' . esc_html($_product->get_sku()) . '</div>';
}
if (!empty($item['variation_id'])) {
echo '<div class="wc-order-item-variation"><strong>' . __('Variation ID:', 'woocommerce') . '</strong> ';
if (!empty($item['variation_id']) && 'product_variation' === get_post_type($item['variation_id'])) {
echo esc_html($item['variation_id']);
} elseif (!empty($item['variation_id'])) {
echo esc_html($item['variation_id']) . ' (' . __('No longer exists', 'woocommerce') . ')';
}
echo '</div>';
}
?>
<input type="hidden" class="order_item_id" name="order_item_id[]" value="<?php echo esc_attr($item_id); ?>" />
<input type="hidden" name="order_item_tax_class[<?php echo absint($item_id); ?>]" value="<?php echo isset($item['tax_class']) ? esc_attr($item['tax_class']) : ''; ?>" />
<?php do_action('woocommerce_before_order_itemmeta', $item_id, $item, $_product) ?>
<?php include( 'html-order-item-meta.php' ); ?>
<?php do_action('woocommerce_after_order_itemmeta', $item_id, $item, $_product) ?>
<?php echo $_product->get_categories(', ', '<div class="wc-order-item-variation"><strong>' . _n('Category:', 'woocommerce:', sizeof(get_the_terms($item['product_id'], 'product_cat')), 'woocommerce') . ' ', '</strong></div>'); ?>
<div style="float: left;" >
<?php
$image_link = wp_get_attachment_image_src(get_post_thumbnail_id($_product->id), 'large');
echo isset($image_link[0]) ? '<img src = "' . $image_link[0] . '" width = "200">' : '';
?>
</div>
<?php
/* Custom code added by Amol */
$post = $_product->post;
$attachment_count = count($_product->get_gallery_attachment_ids());
$gallery = $attachment_count > 0 ? '[product-gallery]' : '';
$props = wc_get_product_attachment_props(get_post_thumbnail_id(), $post);
$image = get_the_post_thumbnail($post->ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'), array(
'title' => $props['title'],
'alt' => $props['alt'],
));
echo apply_filters(
'woocommerce_single_product_image_html', sprintf(
'<a style="text-decoration: none;clear:both;float: left;margin-top: 5px;" href="%s" download = "Order#' . $order->id . '-' . $item['variation_id'] . '"><input type = "button" value="Download image"/></a>', esc_url($props['url'])
), $post->ID
);
//End of custom code by Amol
?>
</td>
<?php do_action('woocommerce_admin_order_item_values', $_product, $item, absint($item_id)); ?>
<td class="item_cost" width="1%" data-sort-value="<?php echo esc_attr($order->get_item_subtotal($item, false, true)); ?>">
<div class="view">
<?php
if (isset($item['line_total'])) {
echo wc_price($order->get_item_total($item, false, true), array('currency' => $order->get_order_currency()));
if (isset($item['line_subtotal']) && $item['line_subtotal'] != $item['line_total']) {
echo '<span class="wc-order-item-discount">-' . wc_price(wc_format_decimal($order->get_item_subtotal($item, false, false) - $order->get_item_total($item, false, false), ''), array('currency' => $order->get_order_currency())) . '</span>';
}
}
?>
</div>
</td>
<td class="quantity" width="1%">
<div class="view">
<?php
echo '<small class="times">×</small> ' . ( isset($item['qty']) ? esc_html($item['qty']) : '1' );
if ($refunded_qty = $order->get_qty_refunded_for_item($item_id)) {
echo '<small class="refunded">' . ( $refunded_qty * -1 ) . '</small>';
}
?>
</div>
<div class="edit" style="display: none;">
<?php $item_qty = esc_attr($item['qty']); ?>
<input type="number" step="<?php echo apply_filters('woocommerce_quantity_input_step', '1', $_product); ?>" min="0" autocomplete="off" name="order_item_qty[<?php echo absint($item_id); ?>]" placeholder="0" value="<?php echo $item_qty; ?>" data-qty="<?php echo $item_qty; ?>" size="4" class="quantity" />
</div>
<div class="refund" style="display: none;">
<input type="number" step="<?php echo apply_filters('woocommerce_quantity_input_step', '1', $_product); ?>" min="0" max="<?php echo $item['qty']; ?>" autocomplete="off" name="refund_order_item_qty[<?php echo absint($item_id); ?>]" placeholder="0" size="4" class="refund_order_item_qty" />
</div>
</td>
<td class="line_cost" width="1%" data-sort-value="<?php echo esc_attr(isset($item['line_total']) ? $item['line_total'] : '' ); ?>">
<div class="view">
<?php
if (isset($item['line_total'])) {
echo wc_price($item['line_total'], array('currency' => $order->get_order_currency()));
}
if (isset($item['line_subtotal']) && $item['line_subtotal'] !== $item['line_total']) {
echo '<span class="wc-order-item-discount">-' . wc_price(wc_format_decimal($item['line_subtotal'] - $item['line_total'], ''), array('currency' => $order->get_order_currency())) . '</span>';
}
if ($refunded = $order->get_total_refunded_for_item($item_id)) {
echo '<small class="refunded">' . wc_price($refunded, array('currency' => $order->get_order_currency())) . '</small>';
}
?>
</div>
<div class="edit" style="display: none;">
<div class="split-input">
<div class="input">
<label><?php esc_attr_e('Pre-discount:', 'woocommerce'); ?></label>
<input type="text" name="line_subtotal[<?php echo absint($item_id); ?>]" placeholder="<?php echo wc_format_localized_price(0); ?>" value="<?php echo $item_subtotal; ?>" class="line_subtotal wc_input_price" data-subtotal="<?php echo $item_subtotal; ?>" />
</div>
<div class="input">
<label><?php esc_attr_e('Total:', 'woocommerce'); ?></label>
<input type="text" name="line_total[<?php echo absint($item_id); ?>]" placeholder="<?php echo wc_format_localized_price(0); ?>" value="<?php echo $item_total; ?>" class="line_total wc_input_price" data-tip="<?php esc_attr_e('After pre-tax discounts.', 'woocommerce'); ?>" data-total="<?php echo $item_total; ?>" />
</div>
</div>
</div>
<div class="refund" style="display: none;">
<input type="text" name="refund_line_total[<?php echo absint($item_id); ?>]" placeholder="<?php echo wc_format_localized_price(0); ?>" class="refund_line_total wc_input_price" />
</div>
</td>
<?php
if (!empty($tax_data)) {
foreach ($order_taxes as $tax_item) {
$tax_item_id = $tax_item['rate_id'];
$tax_item_total = isset($tax_data['total'][$tax_item_id]) ? $tax_data['total'][$tax_item_id] : '';
$tax_item_subtotal = isset($tax_data['subtotal'][$tax_item_id]) ? $tax_data['subtotal'][$tax_item_id] : '';
?>
<td class="line_tax" width="1%">
<div class="view">
<?php
if ('' != $tax_item_total) {
echo wc_price(wc_round_tax_total($tax_item_total), array('currency' => $order->get_order_currency()));
} else {
echo '–';
}
if (isset($item['line_subtotal']) && $item['line_subtotal'] !== $item['line_total']) {
echo '<span class="wc-order-item-discount">-' . wc_price(wc_round_tax_total($tax_item_subtotal - $tax_item_total), array('currency' => $order->get_order_currency())) . '</span>';
}
if ($refunded = $order->get_tax_refunded_for_item($item_id, $tax_item_id)) {
echo '<small class="refunded">' . wc_price($refunded, array('currency' => $order->get_order_currency())) . '</small>';
}
?>
</div>
<div class="edit" style="display: none;">
<div class="split-input">
<div class="input">
<label><?php esc_attr_e('Pre-discount:', 'woocommerce'); ?></label>
<input type="text" name="line_subtotal_tax[<?php echo absint($item_id); ?>][<?php echo esc_attr($tax_item_id); ?>]" placeholder="<?php echo wc_format_localized_price(0); ?>" value="<?php echo esc_attr(wc_format_localized_price($tax_item_subtotal)); ?>" class="line_subtotal_tax wc_input_price" data-subtotal_tax="<?php echo esc_attr(wc_format_localized_price($tax_item_subtotal)); ?>" data-tax_id="<?php echo esc_attr($tax_item_id); ?>" />
</div>
<div class="input">
<label><?php esc_attr_e('Total:', 'woocommerce'); ?></label>
<input type="text" name="line_tax[<?php echo absint($item_id); ?>][<?php echo esc_attr($tax_item_id); ?>]" placeholder="<?php echo wc_format_localized_price(0); ?>" value="<?php echo esc_attr(wc_format_localized_price($tax_item_total)); ?>" class="line_tax wc_input_price" data-total_tax="<?php echo esc_attr(wc_format_localized_price($tax_item_total)); ?>" data-tax_id="<?php echo esc_attr($tax_item_id); ?>" />
</div>
</div>
</div>
<div class="refund" style="display: none;">
<input type="text" name="refund_line_tax[<?php echo absint($item_id); ?>][<?php echo esc_attr($tax_item_id); ?>]" placeholder="<?php echo wc_format_localized_price(0); ?>" class="refund_line_tax wc_input_price" data-tax_id="<?php echo esc_attr($tax_item_id); ?>" />
</div>
</td>
<?php
}
}
?>
<td class="wc-order-edit-line-item" width="1%">
<div class="wc-order-edit-line-item-actions">
<?php if ($order->is_editable()) : ?>
<a class="edit-order-item tips" href="#" data-tip="<?php esc_attr_e('Edit item', 'woocommerce'); ?>"></a><a class="delete-order-item tips" href="#" data-tip="<?php esc_attr_e('Delete item', 'woocommerce'); ?>"></a>
<?php endif; ?>
</div>
</td>
</tr>
Thanks in advance.
— Code updated 2 — (FOUND THE PROBLEMS, see comments in code) —
Just under your custom code in html-order-item.php core file, if you take a look to existing code, you have woocommerce_admin_order_item_values hook that you can use for your code, instead overriding a WooCommerce core file.
But you will also need to use woocommerce_admin_order_item_headers as you will see below in my code snippets…
Because YES you are going to loose your code customization in that PHP file when WooCommerce will be updated. That's why you will find everywhere in WordPress and WooCommerce core files a lot of different hooks to use, for customizations.
You can NOT copy this core files to your active child theme (or active theme) and doing this will not have any effect on it.
What you can do is copy the templates folder located in WooCommerce plugin, to your active child theme (or active theme) and rename it woocommerce. This way you can override the WooCommerce templates via your theme, as explained here in this documentation.
Now, first you are going to replace back the original core file in the plugin.
In your child theme, you normally have a function.php file where you are going to add the code below (a hooked function):
add_action( 'woocommerce_admin_order_item_headers', 'download_image_admin_order_item_headers', 10, 0 );
function download_image_admin_order_item_headers(){
echo '<th class="item sortable" colspan="1" data-sort="string-ins">' . __( 'Downloads', 'woocommerce' ) .'</th>';
}
add_action( 'woocommerce_admin_order_item_values', 'download_image_order_item_values', 10, 3 );
function download_image_order_item_values( $_product, $item, $item_id ){
// Calling global $post to get the order ID
global $post;
// The Order ID
$order_id = $post->ID;
// the Product ID and variation ID (if different of zero for variations)
$product_id = $item['product_id'];
$variation_id = $item['variation_id'];
// If is not a variable product we replace the variation ID by the product ID
if (empty($variation_id)) $variation_id = $product_id;
// HERE ==> Getting an instance of product object, Avoiding an error:
// "Fatal error: Call to a member function get_gallery_attachment_ids()"
$product = new WC_Product($product_id);
// the Product post object
$post_product = $product->post;
$attachment_count = count($product->get_gallery_attachment_ids());
$gallery = $attachment_count > 0 ? '[product-gallery]' : '';
// CODE ERROR ===> This was returning empty before. You need to put
// the product ID in get_post_thumbnail_id() function to get something
$props = wc_get_product_attachment_props(get_post_thumbnail_id($product_id), $post_product);
// Testing $props output (array not empty) => comment/uncomment line below
// echo '<br>ITEM ID: ' . $item_id . '<br><pre>'; var_dump($props); echo '</pre><br>';
$image = get_the_post_thumbnail( $product->id, apply_filters('single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $props['title'],
'alt' => $props['alt'],
));
// Added a condition to avoid other line items than products (like shipping line)
if(!empty($product_id))
echo apply_filters(
'woocommerce_single_product_image_html', sprintf(
'<td class="name" colspan="1" ><a style="text-decoration: none;clear:both;float: left;margin-top: 5px;" href="%s" download = "Order#' . $order_id . '-' . $variation_id . '"><input type = "button" value="Download image"/></a></td>', esc_url($props['url'])
), $product->id
);
}
In Admin Order edit pages on item metabox you will see:
Code goes in function.php file of your active child theme (active theme or in any plugin file).
This code is tested and works.

Shorten PHP code with While Loop

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.

Remove post count from Wordpress custom widget

I'm a noob running a project theme Wordpress website. There is a widget that shows a "Browse by Category" list in columns.
The widget shows the post count in brackets next to the category heading. I want to remove that.
Any help would be greatly appreciated.
This is the code for the widget:
add_action('widgets_init', 'register_browse_by_category_widget');
function register_browse_by_category_widget() {
register_widget('ProjectTheme_browse_by_category');
}
class ProjectTheme_browse_by_category extends WP_Widget {
function ProjectTheme_browse_by_category() {
$widget_ops = array( 'classname' => 'browse-by-category', 'description' => 'Show all categories and browse by category' );
$control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'browse-by-category' );
$this->WP_Widget( 'browse-by-category', 'ProjectTheme - Browse by Category', $widget_ops, $control_ops );
}
function widget($args, $instance) {
extract($args);
echo $before_widget;
if ($instance['title']) echo $before_title . apply_filters('widget_title', $instance['title']) . $after_title;
$loc_per_row = $instance['loc_per_row'];
$widget_id = $args['widget_id'];
$nr_rows = $instance['nr_rows'];
$only_these = $instance['only_these'];
$only_parents = $instance['only_parents'];
if($only_parents == "on") $only_parents = true;
else $only_parents = false;
$nr = 4;
if(!empty($loc_per_row)) $nr = $loc_per_row;
echo '<style>#'.$widget_id.' #location-stuff li>ul { width: '.(round(100/$nr)-0.5).'%}</style>';
if($nr_rows > 0) $jk = "&number=".($nr_rows * $loc_per_row);
$terms_k = get_terms("project_cat","parent=0&hide_empty=0");
$terms = get_terms("project_cat","parent=0&hide_empty=0".$jk);
//$term = get_term( $term_id, $taxonomy );
if($only_these == "1")
{
$terms = array();
foreach($terms_k as $trm)
{
if($instance['term_' . $trm->term_id] == $trm->term_id)
array_push($terms, $trm);
}
}
//-----------------------------
if(count($terms) < count($terms_k)) $disp_btn = 1;
else $disp_btn = 0;
$count = count($terms); $i = 0;
if ( $count > 0 ){
echo "<ul id='location-stuff'>";
foreach ( $terms as $term ) {
if($i%$nr == 0) echo "<li>";
$total_ads = 0;
$terms2 = '';
$terms2 = get_terms("project_cat","parent=".$term->term_id."&hide_empty=0");
$mese = '';
$mese .= '<ul>';
$mese .= "<img src=\"".get_bloginfo('template_url')."/images/posted.png\" width=\"20\" height=\"20\" />
<h3><a class='parent_taxe' rel='taxe_project_cat_".$term->term_id."' href='".get_term_link($term->slug,"project_cat")."'>" . $term->name;
//."</a></h3>";
$total_ads = ProjectTheme_get_custom_taxonomy_count('project',$term->slug);
$mese2 = '';
if($terms2 && $only_parents == false)
{
foreach ( $terms2 as $term2 )
{
$tt = ProjectTheme_get_custom_taxonomy_count('project',$term2->slug);
$total_ads += $tt;
//$mese2 .= "<li><a href='".get_term_link($term2->slug,"project_cat")."'>" . $term2->name." (".$tt.")</a></li>";
$mese2 .= "<li><a href='".get_term_link($term2->slug,"project_cat")."'>" . $term2->name." </a></li>";
}
}
echo $mese."(".$total_ads.")</a></h3>";
echo '<ul id="_project_cat_'.$term->term_id.'">'.$mese2."</ul>";
echo '</ul>';
if(($i+1) % $nr == 0) echo "</li>";
$i++;
}
//if(($i+1) % $nr != 0) echo "</li>";
echo "</ul>";
}
if($disp_btn == 1)
{
echo '<br/><b>'.__('See More Categories','ProjectTheme').'</b>';
}
echo $after_widget;
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function form($instance) { ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
value="<?php echo esc_attr( $instance['title'] ); ?>" style="width:95%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('only_parents'); ?>"><?php _e('Only show parent categories'); ?>:</label>
<input type="checkbox" id="<?php echo $this->get_field_id('only_parents'); ?>" name="<?php echo $this->get_field_name('only_parents'); ?>"
<?php echo (esc_attr( $instance['only_parents'] ) == "on" ? "checked='checked'" : ""); ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('loc_per_row'); ?>"><?php _e('Number of Columns'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('loc_per_row'); ?>" name="<?php echo $this->get_field_name('loc_per_row'); ?>"
value="<?php echo esc_attr( $instance['loc_per_row'] ); ?>" style="width:20%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Number of Rows'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id('nr_rows'); ?>" name="<?php echo $this->get_field_name('nr_rows'); ?>"
value="<?php echo esc_attr( $instance['nr_rows'] ); ?>" style="width:20%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Only show categories below'); ?>:</label>
<?php echo '<input type="checkbox" name="'.$this->get_field_name('only_these').'" value="1" '.(
$instance['only_these'] == "1" ? ' checked="checked" ' : "" ).' /> '; ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('nr_rows'); ?>"><?php _e('Categories to show'); ?>:</label>
<div style=" width:220px;
height:180px;
background-color:#ffffff;
overflow:auto;border:1px solid #ccc">
<?php
$terms = get_terms("project_cat","parent=0&hide_empty=0");
foreach ( $terms as $term ) {
echo '<input type="checkbox" name="'.$this->get_field_name('term_'.$term->term_id).'" value="'.$term->term_id.'" '.(
$instance['term_'.$term->term_id] == $term->term_id ? ' checked="checked" ' : "" ).' /> ';
echo $term->name.'<br/>';
}
?>
</div>
</p>
<?php
}
}
?>
this is the part of code that writes the number of posts by category ...
change this
echo $mese."(".$total_ads.")</a>
with
echo $mese."</a>";

Categories