• Hi all,

    thanks, Davio, for a great plugin. While playing with it I quickly ran into a requirement I had on my site. I needed a widget or shortcode that shows the top ten Yasr rating posts for a certain category only. I was suprised I could not find a request for that here (or maybe I searched wrong) assuming that this was a common need.

    I was about to post a request here when I started playing around with the shortcodes code. It took me a bit but I came up with an addition to the yasr-shortcode-functions.php that does what I wanted. My addition is tailored to the Colormag theme, however, I thought I share it here because I guess others might run into this earlier or later.

    I wrote the following code and added it at the bottom of above mentioned php sript:

    //=============================================================================
    // YASR TOP TEN CATEGORY
    //
    // Shortcode adding the top 10 highest rated posts in one or more categories,
    // supporting themeing. You can add your desired styling as an additional block
    // in the if statement. This code here includes a display for the Colormag theme.
    //
    // Based on:  Yasr 1.4.7 by Davio Curvino
    // Author:    George Lewe <[email protected]>
    // Date:      2017-05-22
    //
    // USAGE
    // [yasr_top_ten_category caption="%catid%" catid="%catid%" color="%color%" rating="%rating%" size="%size%" stars="%stars%" theme="%theme%"]
    //
    // %caption%: In case you want to add a header above the display, you can use 
    //            this parameter.
    //            Default: Yasr Category Top 10
    //
    // %catid%:   One or more category IDs separated by comma
    //            Default: 1
    //
    // %color%:   Some themes support a widget or header color. Use this parameter
    //            to set it, using CSS formatting.
    //            Default: #356aa0
    //
    // %size%:    Size of the Yasr stars
    //            Default: small
    //
    // %rating%:  If you want to display the numeric rating anywhere in your own theme
    //            use this switch to do so.
    //            Default: FALSE
    //
    // %stars%:   If you want to display the stars anywhere in your own theme
    //            use this switch to do so.
    //            Default: FALSE
    //
    // %theme%:   Relates to the if statement block of your custom theme.
    //            Default: yasr
    //
    
    add_shortcode ('yasr_top_ten_category', 'yasr_top_ten_category_callback');
    
       function yasr_top_ten_category_callback ($atts) {
    
          global $wpdb;
    
          // Attributes
          extract( shortcode_atts(
             array(
                'caption' => 'Yasr Category Top 10',
                'catid' => '1',
                'color' => '#356aa0',
                'rating' => FALSE,
                'size' => 'small',
                'stars' => FALSE,
                'theme' => 'yasr'
             ), $atts )
          );
    
          $query_result = $wpdb->get_results("SELECT DISTINCT pm.meta_value AS overall_rating, pm.post_id AS post_id
                                              FROM $wpdb->posts p
                                                 LEFT JOIN $wpdb->postmeta pm ON (p.ID = pm.post_id)
                                                 LEFT JOIN $wpdb->term_relationships ON (p.ID = $wpdb->term_relationships.object_id)
                                                 LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                                              WHERE pm.post_id = p.ID
                                                 AND p.post_status = 'publish'
                                                 AND pm.meta_key = 'yasr_overall_rating'
                                                 AND $wpdb->term_taxonomy.taxonomy = 'category'
                                                 AND $wpdb->term_taxonomy.term_id IN($catid)
                                              ORDER BY pm.meta_value DESC, pm.post_id ASC LIMIT 10");
                                              
          $stars_attribute = yasr_stars_size($size);
    
          $shortcode_html = '
          <!-- Yasr Top 10 Category Shortcode-->
          ';
    
          if ($query_result) {
             
             if ($theme == 'colormag') {
                
                //
                // Display based on the Colormag theme.
                // Displays a vertical list including featured image, title, meta info and excerpt
                //
    
                echo '<div class="so-panel widget widget_colormag_featured_posts_vertical_simple_widget widget_featured_posts widget_featured_posts_vertical widget_featured_meta panel-last-child">';
                echo '<h3 class="widget-title" style="border-bottom-color:'.$color.';"><span style="background-color:'.$color.';">'.$caption.'</span></h3>';
                echo '<div class="following-post">';
             
                foreach ($query_result as $result) {
    
                   echo '<div class="single-article clearfix">';
    
                   //
                   // Featured Image
                   //
                   $link = get_permalink($result->post_id);
                   $featured = 'colormag-featured-post-small';
                   $title_attribute = get_the_title($result->post_id);
                   $image = '';
                   $image .= '<figure>';
                   $image .= '<a href="' . $link . '" title="'.the_title( '', '', false ).'">';
                   $image .= get_the_post_thumbnail( $result->post_id, $featured, array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ) ) ).'</a>';
                   $image .= '</figure>';
                   echo $image;
    
                   //
                   // Title
                   //
                   echo '<div class="article-content">';
                   echo '   <h3 class="entry-title" style="float:left;"><a href="'.$link.'" title="'.$title_attribute.'">'.$title_attribute.'</a></h3>';
                   
                   if ($stars OR $rating) 
                   {
                      $stars_html = '';
                      $rating_html = '';
                      
                      if ($stars)
                      {
                         if ($size=="small") 
                         {
                            $style_stars = "margin: 5px 0 0 12px;";
                         }
                         elseif ($size=="medium") 
                         {
                            $style_stars = "margin: 6px 0 0 12px;";
                         }
                         else 
                         {
                            $style_stars = "margin: 0px 0 0 12px;";
                         }
                         $stars_html = '<div style="'.$style_stars.'" class="'.$stars_attribute['class'].'" data-rateit-starwidth="'.$stars_attribute['px_size'].'" data-rateit-starheight="'.$stars_attribute['px_size'].'" data-rateit-value="'.$result->overall_rating.'" data-rateit-step="0.1" data-rateit-resetable="false" data-rateit-readonly="true"></div>';
                      }
                      
                      if ($rating)
                      {
                         $rating_html = '<span style="color:#888888;font-size: 14px;margin: 0 0 0 10px;">('.$result->overall_rating.')</span>';
                      }
                      
                      echo $stars_html.$rating_html;
                   }
                   
                   echo '</div>';
    
                   //
                   // Meta Info
                   //
                   echo '<div class="below-entry-meta" style="font-size: 11px;color:#888888;">';
                   echo '   <span class="posted-on"><i class="fa fa-calendar-o" style="color:#888888;"></i>&nbsp;&nbsp;'.get_the_date(get_option('date_format'), $result->post_id).'</span>'; // Post Date
                   //echo '   &nbsp;&nbsp;&nbsp;&nbsp;<span class="author vcard"><i class="fa fa-user" style="color:#888888;"></i> '.get_the_author().'</span>'; // Author
                   echo '   &nbsp;&nbsp;&nbsp;&nbsp;<span class="comments"><i class="fa fa-comment" style="color:#888888;"></i><a href="'.$link.'/#respond">&nbsp;&nbsp;'.get_comments_number().'</a></span>'; // Number of comments
                   echo '</div>';
    
                   //
                   // Excerpt
                   //
                   $the_post = get_post($result->post_id);
                   $excerptArray = explode("<!--more-->", $the_post->post_content);
                   if (!isset($excerptArray[1])) $excerpt = substr($excerptArray[0],0,160); else $excerpt = $excerptArray[0];
                   echo '<div style="font-size:14px;">'.strip_tags($excerpt).'</div>';
                   
                   echo '<br>';
                   echo '</div>';
    
                } // End foreach post loop
                
                echo '</div>';
                echo '</div>';
                
             }
             else 
             {
                //
                // Standard Yasr display
                //
    
                $shortcode_html .= "<table class=\"yasr-table-chart\">";
    
                foreach ($query_result as $result) {
    
                   $post_title = get_the_title($result->post_id);
                   $link = get_permalink($result->post_id);
    
                   $shortcode_html .=  "<tr>
                                           <td width=\"60%\" class=\"yasr-top-10-overall-left\"><a href=\"$link\">$post_title</a></td>
                                           <td width=\"40%\" class=\"yasr-top-10-overall-right\">
                                              <div class=\"$stars_attribute[class]\" data-rateit-starwidth=\"$stars_attribute[px_size]\" data-rateit-starheight=\"$stars_attribute[px_size]\" data-rateit-value=\"$result->overall_rating\" data-rateit-step=\"0.1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"true\"></div>
                                              <span class=\"yasr-highest-rated-text\">" . __("Rating", 'yet-another-stars-rating') . " $result->overall_rating </span>
                                           </td>
                                        </tr>";
    
                } // End foreach post loop
    
                $shortcode_html .= "</table>";
    
                $shortcode_html .= '
                <!--End Yasr Top 10 Category Shortcode-->
                ';
             }
    
             return $shortcode_html;
    
          } // End if $query_result
    
          else {
             _e("No posts found with any vote stored.", 'yet-another-stars-rating');
          }
    
       } // End function

    As the commenting header of my code explains, you can use this shortcode to show a sorted list per category (one or more). It sorts the list descending by Yasr rating. It offers the option to add your own themeing or fall back to the Yasr default.

    It is surely not perfect but maybe a feature like that will find it’s way into the next release.

    Thanks again, Davio, for an excellent pluing.

    Best regards,
    George

  • The topic ‘Sort by category and Yasr rating’ is closed to new replies.