Include recipe rating on archive pages

Star ratings are a great way to encourage your visitors to read your recipes and share their own experience. WP Recipe Maker makes it easy to let readers add their rating.

Use the [wprm-recipe-rating] shortcode to display a recipe’s rating. If you want to build it into your theme, edit the single.php file and add this where you’d like the star rating to appear:

echo do_shortcode( '[wprm-recipe-rating]' );

This works great on single posts because the shortcode will find the first recipe in the first post on the page. But if you call this shortcode on an archive page, it will display the first post’s rating for every post.

The way to fix this is to specify the specific recipe ID in the shortcode. We can search the post content for the recipe, and if a recipe is found, output the star rating using that recipe’s ID.

Add this function to your theme’s functions.php file.

/**
 * Star Rating 
 * @see https://www.billerickson.net/include-recipe-rating-on-archive-pages
 *
 */
function be_star_rating() {

	// Return early if WP Recipe Maker isn't active 
	if( ! class_exists( 'WPRM_Recipe_Manager' ) )
		return;

	global $post;
	$recipes = WPRM_Recipe_Manager::get_recipe_ids_from_content( $post->post_content );
	if ( isset( $recipes[0] ) ) {
		$recipe_id = $recipes[0];
		echo do_shortcode( '[wprm-recipe-rating id="' . $recipe_id . '"]' );
	}

}

Then call be_star_rating() where you want the star rating to appear. Here’s how I use it in the template partial used in the screenshot above:

echo '<article class="post-summary">';
	echo '<a class="entry-image-link" href="' . get_permalink() . '" tabindex="-1" aria-hidden="true">' . get_the_post_thumbnail( get_the_ID(), 'small_thumbnail' ) . '</a>';

	echo '<header class="post-summary__content">';
		be_star_rating();
		echo '<h4 class="entry-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>';
	echo '</header>';
echo '</article>';

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Leave A Reply