pratish7 (November 4th, 2010)
<?php
/**
* Add function to widgets_init that'll load our widget.
*/
add_action( 'widgets_init', 'CP_Cities_load_widgets' );
/**
* Register our widget.
*/
function CP_Cities_load_widgets() {
register_widget( 'CP_Cities_Widget' );
}
/**
* CP_Cities_Widget class.
*/
class CP_Cities_Widget extends WP_Widget {
/**
* Widget setup.
*/
function CP_Cities_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'city', 'description' => __('An widget that displays Cities', 'example') );
/* Create the widget. */
$this->WP_Widget( 'CP-cities', __('CP City Widget', 'example'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title'] );
$count = isset( $instance['count'] ) ? $instance['count'] : 5;
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
global $wpdb;
$cities = array();
$sql = $wpdb->prepare("SELECT * FROM `{$wpdb->prefix}cp_ad_pop_total` t
LEFT JOIN $wpdb->postmeta m
ON t.postnum=m.post_id
WHERE m.meta_key='cp_city' ORDER BY t.postcount desc");
$results = $wpdb->get_results($sql);
foreach($results as $r)
{
array_push($cities, $r->meta_value);
}
$cities = array_unique($cities);
$cities = array_values($cities);
$cities_n = count($cities) - 1;
for ($i=0 ; $i <= $cities_n ; $i++)
{
if ( ($i%2) == 0)
$cities_left = $cities_left . '<li><a href="?s=' . urlencode($cities[$i]) . '">' . $cities[$i] . '</a></li>';
else
$cities_right = $cities_right . '<li><a href="?s=' . urlencode($cities[$i]) . '">' . $cities[$i] . '</a></li>';
}
echo '<ul class="sidebar-left-col">';
echo $cities_left;
echo '</ul>';
echo '<ul class="sidebar-right-col">';
echo $cities_right;
echo '</ul>';
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => '');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<?php
}
}
?>
include_once 'widget-city.php';
pratish7 (November 4th, 2010)
ahikmahin (November 2nd, 2010)
There are currently 1 users browsing this thread. (0 members and 1 guests)