how to create custom popular post widget in WordPress

How to create custom popular post widget in WordPress? OR how to you create custom popular posts widget for my website? It is very easy to create a custom popular posts widget for your own website. It is some simple code that you just copy and paste your website. So, now we go to create custom widget for popular posts.

First open your theme folder and open your function.php / theme-function.php / custom-widget.php file. now copy below code and paste your function.php / theme-function.php / custom-widget.php file. Update your file and go to Widget option you can see here a new widget for custom popular posts widget “Br Theme – Popular Posts”. Now this widget use any where in your widget area.

php code for create custom popular post widget

///POPULAR POST WIDGET

class brtheme_popular_posts extends WP_Widget {
function __construct() {
$widgets_opt = array(‘description’=>’Display popular posts’);
parent::__construct(false,$name= “BR Theme – Popular Posts Widget”,$widgets_opt);
}
function form($instance) {
global $post;
$title = isset( $instance[‘title’] ) ? esc_attr( $instance[‘title’] ) : ”;
$count = isset( $instance[‘count’] ) ? esc_attr( $instance[‘count’] ) : ”;

?>
<p><label for=”<?php echo $this->get_field_id(‘title’); ?>”>Title:
<input id=”<?php echo $this->get_field_id(‘title’); ?>” name=”<?php echo $this->get_field_name(‘title’); ?>” type=”text” class=”widefat” value=”<?php echo $title;?>” /></label></p>

<div style=”float:left; width:99%;”>
<p><label for=”<?php echo $this->get_field_id(‘count’); ?>”>Show Item:
<input id=”<?php echo $this->get_field_id(‘count’); ?>” name=”<?php echo $this->get_field_name(‘count’); ?>” type=”text” class=”widefat” value=”<?php echo $count;?>” /></label></p>
</div>
<?php
}
function update($new_instance, $old_instance) {
return $new_instance;
}
function widget( $args, $instance ) {
global $post;
extract($args);
$title = apply_filters(‘widget_title’, $instance[‘title’]);
$count = apply_filters(‘count’, $instance[‘count’]);

echo $before_widget;
if ( $title )
echo ‘<h2>’. $title. ‘</h2>’;

$cID = get_the_ID();

function BRTheme_popular_posts($post_id) {
$count_key = ‘popular_posts’;
$count = get_post_meta($post_id, $count_key, true);
if ($count == ”) {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, ‘0’);
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
function BRTheme_track_posts($post_id) {
if (!is_single()) return;
if (empty($post_id)) {
global $post;
$post_id = $post->ID;
}
BRTheme_popular_posts($post_id);
}
add_action(‘wp_head’, ‘BRTheme_track_posts’);

$popular = new WP_Query(array(‘posts_per_page’=>$count, ‘meta_key’=>’popular_posts’, ‘orderby’=>’meta_value_num’, ‘order’=>’DESC’));;
$i=1;
//query_posts($query);

if (have_posts()) :
echo ‘<div class=”popular-posts-items”>’;
//while (have_posts()) : the_post(); $i++;
while ($popular->have_posts()) : $popular->the_post();;

echo ‘<div class=”popular-post-item”>’;

echo ‘<h5><a href=”‘. get_the_permalink() .'” >’.$i++.’. ‘ .get_the_title().'</a></h5>’;
echo ‘<p>’ .wp_trim_words( get_the_content(”), ’12’, ” ).'</p>’;
echo ‘</div>’;
endwhile;
echo ‘<div class=”clr”></div>’; endif; wp_reset_query();

echo $after_widget;
}
}
add_action(‘widgets_init’, create_function(”, ‘return register_widget(“brtheme_popular_posts”);’));

​Leave a Comment

Comment authors age:




how to create custom category widget in wordpress?