geminilabs

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

Apr 23rd, 2021 (edited)
918
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 this a single page?
  13.             || is_home()     // is this the blog page?
  14.             || is_archive()  // is this a post archive page?
  15.             || is_category() // is this a category page?
  16.         )) {
  17.         $shortcode = '[site_reviews_summary assigned_posts=post_id hide="bars,rating,summary"]';
  18.         $shortcode = do_shortcode($shortcode);
  19.         $shortcode = str_replace(['<div', '/div>'], ['<span', '/span>'], $shortcode); // replace block-level elements
  20.         return sprintf('<span>%s</span>%s', $title, $shortcode);
  21.     }
  22.     return $title;
  23. }, 10, 2);
RAW Paste Data Copied