SHARE
TWEET

Untitled

a guest Oct 15th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     function my_rating( $atts ) {
  3.         $rating = shortcode_atts( array(
  4.             'stars'  => '',
  5.             'half'   => 'false',
  6.         ), $atts );
  7.  
  8.         $star      = esc_attr($rating['stars']);
  9.         $stars     = str_repeat('<i class="fas fa-fw fa-star"></i>', $star);
  10.         $half      = esc_attr($rating['half']);
  11.         $halfstar  = '<i class="fas fa-fw fa-star-half-alt"></i>';
  12.         $dif       = 5 - esc_attr($rating['stars']);
  13.         $empty     = str_repeat('<i class="far fa-fw fa-star"></i>', $dif);
  14.         $difhalf   = 4 - esc_attr($rating['stars']);
  15.         $emptyhalf = str_repeat('<i class="far fa-fw fa-star"></i>', $difhalf);
  16.  
  17.         if ( $half == 'false' ) {
  18.             return $stars . $empty . '<span class="hide" aria-hidden="false">' . $star .'.0 out of 5</span> <span class="lining" aria-hidden="true">' . $star .'.0</span>';
  19.         }
  20.         elseif ( ( $half == 'true' ) && ( $star < 5 ) ) {
  21.             return $stars . $halfstar . $emptyhalf . '<span class="hide" aria-hidden="false">' . $star .'.5 out of 5</span> <span class="lining" aria-hidden="true">' . $star .'.5</span>';
  22.         } else {
  23.             // 5 stars is maximum. 5½ stars outputs 5 stars.
  24.             return $stars . $empty . '<span class="hide" aria-hidden="false">5.0 out of 5</span> <span class="lining" aria-hidden="true">5.0</span>';
  25.         }
  26.  
  27.     }
  28.  
  29.     add_shortcode( 'rating', 'my_rating' );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top