Latest Post Widget
class Latest_Posts_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'latest-posts',
__( 'Latest Posts', 'text_domain' ),
array(
'description' => __( 'Show the 2 latest posts', 'text_domain' ),
'classname' => 'latest-post-widget',
)
);
}
public function widget( $args, $instance ) {
$args = array (
'post_type' => array( 'post' ),
'nopaging' => false,
'posts_per_page' => '2',
);
$output = '';
$colnum = '1';
// The Query
$latest_query = new WP_Query( $args );
if ( $latest_query->have_posts() ) {
$output .= '<div class="section the-post clearfix">';
while ( $latest_query->have_posts() ) : $latest_query->the_post();
$attachment_id = get_post_thumbnail_id( $post->ID );
$src = wp_get_attachment_image_src( $attachment_id, 'large' );
$output .= '<div class="column one-second';
if($colnum %2 == 0) { $output .= ' clearfix'; }
$output .= '">';
$output .= '<div class="section clearfix">';
$output .= '<a href="'. get_permalink() .'" style="display:block;">';
$output .= '<div class="rounded-edges post-thumbnail column one img-16-9" style="background: url('.$src[0].') center no-repeat !important; background-size:cover !important;"></div>';
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="section">';
$output .= '<div class="column one post-meta-noinline">';
$output .= '<div class="section clearfix">';
$output .= '<div class="column one post-catagory">';
$output .= '<div class="latest-post-catagories"> ';
foreach((get_the_category()) as $category) { $output .= $category->cat_name . ' '; }
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '<div class="section clearfix">';
$output .= '<div class="column one post-catagory">';
$output .= '<div class="latest-post-title">';
$output .= '<div style="text-align: left"><a href="'. get_permalink() .'">'. get_the_title() .'</a></div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$colnum++;
endwhile;
$output .= '</div>';
return $output;
}
wp_reset_postdata();
}
public function form( $instance ) {
}
public function update( $new_instance, $old_instance ) {
}
}
function lpw_register_widgets() {
register_widget( 'Latest_Posts_Widget' );
}
add_action( 'widgets_init', 'lpw_register_widgets' );