Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Registers the [site_reviews_rating] shortcode
- * This shortcode displays the average star rating of a page or category that has reviews assigned to them.
- *
- * Shortcode options are as follows:
- * - [site_reviews_rating category=""] Optional. Allowed values: category slug or ID
- * - [site_reviews_rating assigned_to=""] Optional. Allowed values: post/page ID or "post_id" to use the current page ID
- *
- * Note: the "assigned_to" option will take precedence over the "category" option, you should only use one or the other.
- *
- * Paste this in your active theme's functions.php file
- * @param array $atts
- * @return int
- */
- add_shortcode('site_reviews_rating', function ($atts) {
- $atts = wp_parse_args($atts, [
- 'assigned_to' => '',
- 'category' => '',
- ]);
- $rating = 0;
- if (!empty($atts['category'])) {
- if ($term = term_exists($atts['category'], 'site-review-category')) {
- $rating = get_term_meta($term['term_id'], '_glsr_average', true);
- }
- }
- if (!empty($atts['assigned_to'])) {
- $postId = $atts['assigned_to'] == 'post_id'
- ? get_the_ID()
- : $atts['assigned_to'];
- $rating = get_post_meta($postId, $metaKey, true);
- }
- return apply_filters('glsr_star_rating', $rating, $rating);
- });
Add Comment
Please, Sign In to add comment