Created
June 30, 2015 19:59
-
-
Save braginteractive/c0d3460b3913cbadaf41 to your computer and use it in GitHub Desktop.
Displays posts with featured images in WordPress widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Creating the widget | |
class name_prefix_widget extends WP_Widget { | |
function __construct() { | |
parent::__construct( | |
// Base ID of your widget | |
'name_prefix_widget', | |
// Widget name will appear in UI | |
__('Recent Posts with Image', 'text-domain'), | |
// Widget description | |
array( | |
'description' => __( 'Show post with featured image.', 'text-domain' ), | |
) | |
); | |
} | |
// Creating widget front-end | |
// This is where the action happens | |
public function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters( 'widget_title', $instance['title'] ); | |
// before and after widget arguments are defined by themes | |
echo $before_widget; | |
if ( ! empty( $title ) ) | |
echo $before_title . $title . $after_title; | |
// This is where you run the code and display the output | |
$number = $instance['number']; | |
$order = $instance['order']; | |
$args = array( | |
'posts_per_page' => $number, | |
'orderby' => $order, | |
'post__not_in' => array( get_the_ID() ), | |
); | |
$my_query = new WP_Query($args); | |
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?> | |
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | |
<div class="row"> | |
<?php if ( has_post_thumbnail() ) { ?> | |
<div class="col-xs-5"> | |
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> | |
<?php the_post_thumbnail('full', array( 'class' => 'img-responsive img-margin' )); ?> | |
</a> | |
</div> | |
<div class="col-xs-7"> | |
<?php } else { ?> | |
<div class="col-sm-12"> | |
<?php } ?> | |
<header class="entry-header"> | |
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> | |
<?php if ( 'post' == get_post_type() ) : ?> | |
<div class="entry-meta"> | |
<?php name_prefix_posted_in(); ?> | |
</div><!-- .entry-meta --> | |
<?php endif; ?> | |
</header><!-- .entry-header --> | |
</div> | |
</div> | |
</article><!-- #post-## --> | |
<?php | |
endwhile; // end of one post | |
endif; //end of loop | |
wp_reset_query(); // reset the query | |
echo $after_widget; | |
} | |
// Widget Backend | |
public function form( $instance ) { | |
$instance = wp_parse_args( (array) $instance, array( | |
'title' => __('Recent Posts','text-domain' ), | |
'number' => '3', | |
'order' => 'ASC', | |
) ); | |
$title = esc_attr($instance['title']); | |
$number = esc_attr($instance['number']); | |
$order = esc_attr( $instance['order'] ); | |
// Widget admin form | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'text-domain' ); ?></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 esc_attr( $title ); ?>" /> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('order'); ?>"><?php _e( 'Random or Recent?', 'text-domain' ); ?></label> | |
<br /> | |
<select class='name_prefix-select' name="<?php echo $this->get_field_name('order'); ?>" id="<?php echo $this->get_field_id('order'); ?>"> | |
<option value="ASC" <?php if($order == 'ASC') { ?>selected="selected"<?php } ?>><?php _e( 'Recent', 'text-domain' ); ?></option> | |
<option value="rand" <?php if($order == 'rand') { ?>selected="selected"<?php } ?>><?php _e( 'Random', 'text-domain' ); ?></option> | |
<option value="comment_count" <?php if( $order == 'comment_count' ) { ?>selected="selected"<?php } ?>><?php _e( 'Most Comments', 'text-domain' ); ?></option> | |
<option value="modified" <?php if( $order == 'modified' ) { ?>selected="selected"<?php } ?>><?php _e( 'Last Modified', 'text-domain' ); ?></option> | |
</select> | |
</p> | |
<p> | |
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'Number to Show', 'text-domain' ); ?></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> | |
<?php } | |
// Updating widget replacing old instances with new | |
public function update( $new_instance, $old_instance ) { | |
$instance = array(); | |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; | |
$instance['number'] = strip_tags($new_instance['number']); | |
$instance['order'] = strip_tags($new_instance['order']); | |
return $instance; | |
} | |
} // Class name_prefix_widget ends here | |
// Register and load the widget | |
function name_prefix_load_widget() { | |
register_widget( 'name_prefix_widget' ); | |
} | |
add_action( 'widgets_init', 'name_prefix_load_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment