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

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

  1. Have you ever thought about including a little bit more than just your articles?
    I mean, what you say is important and everything.
    But imagine if you added some great photos or video clips to give your posts more, “pop”!
    Your content is excellent but with images and videos,
    this blog could definitely be one of the most beneficial in its
    field. Great blog!

  2. This post is actually a good one it helps new the web visitors, who are wishing for blogging. http://decorativeconcretecompani13578.post-blogs.com

  3. This post is actually a good one it helps new the web visitors, who are wishing for
    blogging. http://decorativeconcretecompani13578.post-blogs.com

  4. 3win8 apk says:

    Next ask these types of perform the skill they visualized.
    But the sheer numbers of people which just plain got taken was skyrocketing.
    Chapter 7 makes in a situation for making science on the job. https://scr888.group/other-games-download/2492-download-3win8

  5. We offer to you a simple guide steps to help you in this regard.
    Good content while a user friendly interface are
    primary for the SEO efficient. Kim has 392 fans
    and keeps them well entertained and well-informed. https://918kiss.host/75-rollex11

  6. We offer to you a simple guide steps to help you
    in this regard. Good content while a user friendly interface are primary for the SEO efficient.
    Kim has 392 fans and keeps them well entertained and
    well-informed. https://918kiss.host/75-rollex11

​Leave a Comment

Comment authors age:




how to create custom category widget in wordpress?

Some of Home Based Business Idea