Skip to content

Instantly share code, notes, and snippets.

@alokstha1
Created September 29, 2015 04:12
Show Gist options
  • Save alokstha1/28cbc1ce15f7c2db5607 to your computer and use it in GitHub Desktop.
Save alokstha1/28cbc1ce15f7c2db5607 to your computer and use it in GitHub Desktop.
Wordpress widget to get posts
<?php
function my_widget() {
register_widget('Widget_Name');
}
add_action('widgets_init', 'my_widget');
/**
* Widget controls
*/
class Widget_Name extends WP_Widget
{
function __construct() {
parent::__construct(
'post_widget', // Base ID
__( 'Title', 'text_domain' ), // Name
array( 'description' => __( 'Description about the widget', 'text_domain' ), ) // Args
);
}
function widget( $args, $instance ) {
echo $args['before_title']. $args['widget_name'].$args['after_title'];
$post_type_args = array(
'post_type' => 'post',
'posts_per_page' => 3,
);
$query = new WP_Query($post_type_args); ?>
<div class="list-group">
<?php
if($query->have_posts()):
while ($query->have_posts()):
$query->the_post(); ?>
<a class="list-group-item" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php
endwhile;
wp_reset_postdata();
endif; ?>
</div>
<?php
}
function update( $new_instance, $old_instance ) {
}
function form( $instance ) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment