"Latest Pictures",
'Picture Count' => 10,
'Picture Link' => "post",
'Picture Category' => 0
);
}
function widget_attachview($args) {
global $wpdb;
extract($args);
// Each widget can store and retrieve its own options.
// Here we retrieve any options that may have been set by the user
// relying on widget defaults to fill the gaps.
$options = array_merge(widget_attachview_options(), get_option('widget_attachview'));
unset($options[0]); //returned by get_option(), but we don't need it
$query = "SELECT DISTINCT * FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy ";
$query .= "WHERE $wpdb->posts.post_type LIKE 'attachment' ";
$query .= "AND $wpdb->posts.post_mime_type REGEXP 'image' ";
$query .= "AND ($wpdb->posts.post_parent = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = " . $options['Picture Category']. ") ";
$query .= "GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC ";
$query .= "LIMIT ".$options['Picture Count'];
$attachments = $wpdb->get_results($query);
echo $before_widget . $before_title . $options['Title'] . $after_title;
$counter = 0;
$attachment_list = "
";
foreach($attachments as $attach) {
$attachment_list .= "
";
echo $attachment_list;
echo $after_widget;
}
// This is the function that outputs the form to let the users edit
// the widget's title. It's an optional feature that users cry for.
function widget_attachview_control() {
// Each widget can store and retrieve its own options.
// Here we retrieve any options that may have been set by the user
// relying on widget defaults to fill the gaps.
if(($options = get_option('widget_attachview')) === FALSE) $options = array();
$options = array_merge(widget_attachview_options(), $options);
unset($options[0]); //returned by get_option(), but we don't need it
// If user is submitting custom option values for this widget
if ( $_POST['attachview-submit'] ) {
// Remember to sanitize and format use input appropriately.
foreach($options as $key => $value)
if(array_key_exists('attachview-'.sanitize_attachment($key), $_POST))
$options[$key] = strip_tags(stripslashes($_POST['attachview-'.sanitize_attachment($key)]));
// Save changes
update_option('widget_attachview', $options);
}
// Title option
echo '';
// Picture count option
echo '';
// Link option
echo '';
// Category option
$categories = get_categories();
echo '';
// Submit
echo '';
}
// This registers our widget so it appears with the other available
// widgets and can be dragged and dropped into any active sidebars.
register_sidebar_widget('Latest Pictures', 'widget_attachview');
// This registers our optional widget control form.
register_widget_control('Latest Pictures', 'widget_attachview_control');
}
// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded', 'widget_attachview_init');
?>