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”);’));

19 Responses to “how to create custom popular post widget in WordPress”

  1. minecraft says:

    I enjoy what you guys are up too. This sort of clever work and exposure!
    Keep up the very good works guys I’ve added you guys to
    my blogroll.

  2. minecraft says:

    There is definately a great deal to find out about this subject.
    I really like all the points you have made.

  3. minecraft says:

    Hello, I enjoy reading through your article. I like to write a little comment to support you.

  4. minecraft says:

    Hi, I think your site might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a quick heads up!
    Other then that, fantastic blog!

  5. minecraft says:

    I am curious to find out what blog platform you have been using?
    I’m having some small security issues with my latest blog and
    I would like to find something more risk-free. Do you have any solutions?

  6. minecraft says:

    Hey! I could have sworn I’ve been to this blog before but after
    checking through some of the post I realized it’s new to me.
    Nonetheless, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  7. minecraft says:

    This is my first time visit at here and i am in fact
    pleassant to read everthing at single place.

  8. minecraft says:

    I pay a quick visit daily a few blogs and blogs to read articles, but this blog presents
    quality based writing.

  9. minecraft says:

    I have read so many articles or reviews about the blogger lovers but
    this article is in fact a pleasant piece of writing,
    keep it up.

  10. minecraft says:

    Wow, that’s what I was exploring for, what a information! present here
    at this website, thanks admin of this web page.

  11. Hey there are using WordPress for your site platform? I’m new
    to the blog world but I’m trying to get started and set up
    my own. Do you need any html coding expertise to make your own blog?
    Any help would be greatly appreciated!

  12. It’s a shame you don’t have a donate button! I’d most certainly donate to this superb
    blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will talk about
    this blog with my Facebook group. Talk soon!

  13. Terrific work! This is the type of info that are supposed to
    be shared around the internet. Disgrace on Google for not positioning
    this post higher! Come on over and seek advice from my site .
    Thanks =)

  14. Its like you read my mind! You seem to know so much about this, like you
    wrote the book in it or something. I think that you can do with some pics to
    drive the message home a little bit, but instead of that,
    this is magnificent blog. A fantastic read.
    I will certainly be back.

  15. If you want to grow your experience only keep visiting this website and be
    updated with the most recent gossip posted
    here.

  16. Heya i am for the first time here. I came across
    this board and I to find It truly useful & it helped me out a lot.
    I am hoping to give one thing back and aid others such
    as you helped me.

  17. Hey there! I simply wish to offer you a huge thumbs up for
    the excellent info you have got here on this post. I’ll be coming back to your
    blog for more soon.

  18. I was suggested this blog by my cousin. I’m not sure whether this post is written by him
    as no one else know such detailed about my trouble.
    You are amazing! Thanks!

​Leave a Comment

Comment authors age:




how to create custom category widget in wordpress?

Some of Home Based Business Idea