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 the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Daniel Dessinger says

    I love the idea of including individual recipe ratings on Category or Archive pages. I’m going to reread this a few times to get a better feel for how to implement this.

    While I’m here, do you know of any way to disable comment ratings on non-recipe posts? I just realized that every post we publish has the recipe ratings above the comment field and I would only want them to appear on a post containing a recipe.

    • Bill Erickson says

      It depends on how your comment rating field is being added to your site. I think WP Recipe Maker only adds it to the comment form if the current post contains a recipe. If you’re using a different plugin, you might check with the plugin author for how to disable it on non-recipe posts.

  2. Jürgen Bruckner says

    Is it possible to automatically refresh the page’s cache after a review?
    For me, the rating is often only displayed after the cache has been updated.

    • Bill Erickson says

      This is something you’d need to configure in your caching plugin. Most caching plugins only purge the archive caches when a post is created/edited, not when comments are made.