geminilabs

[site-reviews] add rating to CPT page title on archive and single pages

Apr 23rd, 2021 (edited)
703
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Append the Site Reviews rating summary stars to the page title of Posts on single and archive pages
  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_post_type_archive($postTypes))) {
  13.         $shortcode = '[site_reviews_summary assigned_to=post_id hide="bars,rating,summary"]';
  14.         $shortcode = do_shortcode($shortcode);
  15.         $shortcode = str_replace(['<div', '/div>'], ['<span', '/span>'], $shortcode); // replace block-level elements
  16.         return sprintf('<span>%s</span>%s', $title, $shortcode);
  17.     }
  18.     return $title;
  19. }, 10, 2);
RAW Paste Data