geminilabs

Untitled

Apr 26th, 2021 (edited)
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. /**
  2.  * Append the Site Reviews rating summary stars, if they exist, to the page title
  3.  * Paste this in your active theme's functions.php file.
  4.  * @param string $title
  5.  * @param int $postId
  6.  * @return string
  7.  */
  8. add_filter('the_title', function ($title, $postId) {
  9.     $postTypes = ['post']; // change this as needed
  10.     if (in_the_loop()
  11.         && in_array(get_post_type($postId), $postTypes)
  12.         && (is_single() || is_home() || is_category() || is_post_type_archive($postTypes))) {
  13.         $shortcode = do_shortcode('[site_reviews_summary assigned_to="post_id" hide="bars,rating" text="<span class=\'num-stars\'>{rating} stars from</span> {num} reviews!"]');
  14.         $shortcode = str_replace(['<div', '/div>'], ['<span', '/span>'], $shortcode); // replace block-level elements
  15.         return sprintf('<span>%s</span>%s', $title, $shortcode);
  16.     }
  17.     return $title;
  18. }, 10, 2);
Add Comment
Please, Sign In to add comment