Widget showing most popular of all post types in given time period, not all time
-
Hi, and thanks for a great plugin!
The default widget shows the top rated entries of all time, and also limits the output to one post type at the time.
I needed to show the most popular entries in the last couple of weeks, and also mix in all post types in the same list.
So, I put together a little something and inserted in my themes functions.php file. So, I figured I’d try to do my part and share the code if others need the same feature.
This code adds a new widget. In the widget control panel you can set a widget title, specify which time period you want to base your list on, and also limit the number of entries shown.
As the code clearly shows I’m only a php beginner, so if anyone with a php knowledge beyond copy and paste wants to improve on the code, feel free to do so. Also, the comments and names are in norwegian. I hope the code is still readable.
I also hope I’m not violating any license agreements or anything by posting this here. If so, please delete my entry and forgive my ignorance.
/** * Adds widget. */ class Poppis_Widget extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { parent::__construct( 'poppis_widget', // Base ID __('Popular - CRFP', 'poppiswidget'), // Name array( 'description' => __( 'Show most popular entries across all post types', 'poppiswidget' ), ) // Args ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { $title = apply_filters( 'widget_title', $instance['title'] ); $range = apply_filters( 'widget_title', $instance['range'] ); $limit = apply_filters( 'widget_title', $instance['limit'] ); echo $args['before_widget']; if ( ! empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; $com_args = array( 'date_query' => array( 'after' => "$range ago", 'before' => 'tomorrow', 'inclusive' => true, ), 'orderby' => 'comment_post_ID', ); $poppis_neste = get_comments($com_args); $forrige_id = 0; // ID paa innlegget til forrige kommentar $count = 0; // Antall vurderinger pr innlegg $postsum = 0; // Sum av vurderinger $snittvurderinger = array(); foreach ($poppis_neste as $kommentar) { $rating = get_comment_meta($kommentar->comment_ID,'crfp',1); $ratingegen; foreach ($rating as $rat) { // echo $rat; $ratingegen = $rat; } // Hvis samme ID som sist: Legge sammen og oeke teller if ($kommentar->comment_post_ID == $forrige_id) { // SAMME SOM FORRIGE // echo "<br>SAMME (" . $kommentar->comment_post_ID . "/" . $forrige_id . ")"; $postsum = $postsum + $ratingegen; $count++; $forrige_id = $kommentar->comment_post_ID; // echo "Count " . $count . " -- postsum " . $postsum . "<br>"; } else { // NY! // Regne snitt og legge i array if ($postsum) { // Bare legge i array hvis vi faktisk har noe -- dvs ogsaa hoppe over foerste nye, som er tom $snitt = $postsum / $count; // Lagre til array $snittvurderinger[$forrige_id] = $snitt; // Tomme variablene til neste innlegg $count = 0; $postsum = 0; $postsum = $postsum + $ratingegen; $count++; // echo "Count " . $count . " -- postsum " . $postsum . "<br>"; } else { // Hvis foerste gang skal vi gjoere samme som ellers $postsum = $postsum + $ratingegen; $count++; // echo "Count " . $count . " -- postsum " . $postsum . "<br>"; } // Sette id saa neste sjekk blir riktig $forrige_id = $kommentar->comment_post_ID; } } // foreach $snittvurderinger[$forrige_id] = $snitt; arsort($snittvurderinger); $count = 0; echo "<ul>"; foreach ($snittvurderinger as $key => $val) { if ($count < $limit) { // Ikke vise flere enn bestemt limit //////////////////////// // EDIT THE FOLLOWING TO CHANGE HOW THE LIST LOOKS // Remember that you have no object, only $key as the ID of each post, so you need to use the correct functions, // e.g. "get_the_title($key);" instead of "the_title();". ?> <li> <div class="bilde"> <?php $bilde = get_the_post_thumbnail($key); $type = get_post_type($key); if ($bilde) { $visbilde = get_the_post_thumbnail($key,'thumbnail'); echo $visbilde; } else { $defaultbilde = '<img src="URL-TO-DEFAULT-IMAGE.png">'; echo $defaultbilde; } ?> </div> <div class="innhold"> <div class="tittel"> <a href="<?php echo get_the_permalink($key) ?>" rel="bookmark"><?php echo get_the_title($key); ?></a> </div> <div class="rating"> <a href="<?php the_permalink($key); ?>#comments"> <div class="rating-container"> <span class="label">Rating</span> <span class="rating-always-on"> <span class="crfp-rating crfp-rating-<?php $rate = round($val * 2) / 2; echo str_replace(".","-",$rate); ?>"><?php echo $val; ?></span> </span> </div> </a> </div> </div> </li> <?php // STOP EDITING :) //////////////////////// $count++; // Legge til counter } // If lavere enn limit } // Loope gjennom hvert innlegg echo "</ul>"; echo $args['after_widget']; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { // Title if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'poppiswidget' ); } // Range if ( isset( $instance[ 'range' ] ) ) { $range = $instance[ 'range' ]; } else { $range = __( '4 week', 'poppiswidget' ); } // Limit if ( isset( $instance[ 'limit' ] ) ) { $limit = $instance[ 'limit' ]; } else { $limit = __( '3', 'poppiswidget' ); } ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> </p> <p> <label for="<?php echo $this->get_field_id( 'range' ); ?>"><?php _e( 'Range:' ); ?></label> <p>E.g "4 week", "1 month", etc</p> <input class="widefat" id="<?php echo $this->get_field_id( 'range' ); ?>" name="<?php echo $this->get_field_name( 'range' ); ?>" type="text" value="<?php echo esc_attr( $range ); ?>"> </p> <p> <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Limit to # of posts:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo esc_attr( $limit ); ?>"> </p> <?php } /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; $instance['range'] = ( ! empty( $new_instance['range'] ) ) ? strip_tags( $new_instance['range'] ) : ''; $instance['limit'] = ( ! empty( $new_instance['limit'] ) ) ? strip_tags( $new_instance['limit'] ) : ''; return $instance; } } // class Foo_Widget // register Foo_Widget widget function register_poppis_naa_widget() { register_widget( 'Poppis_Widget' ); } add_action( 'widgets_init', 'register_poppis_naa_widget' );
https://www.ads-software.com/plugins/comment-rating-field-plugin/
- The topic ‘Widget showing most popular of all post types in given time period, not all time’ is closed to new replies.