1. Home
  2. Knowledge Base
  3. Tasty Recipes
  4. Customizing Tasty Recipes
  5. Filter: Place recipe rating at the top of the post

Filter: Place recipe rating at the top of the post

If you're interested placing your recipe's ratings at the top of your posts, you can use this filter to add it in.

Just add in the following snippet to your theme's functions.php file:

***Before doing this, make sure that you have access to your site's files via FTP (the editor in WordPress is not sufficient in case problems arise) and that you are comfortable with editing your functions.php file. Any minor error in the functions.php file can result in the “white screen of death,” which is easily fixed via FTP but is difficult to fix otherwise (and a nuisance as your site will be unavailable until fixed).***

add_filter( 
    'the_content', 
    function( $content ) { 
        return trx_recipe_rating_html( get_the_ID() ) . $content; 
    }
); 

/** 
* Generates rating markup for use elsewhere in a post. 
* 
* @param integer $post_id ID for the post. 
* @return string 
*/
function trx_recipe_rating_html( $post_id ) { 
    if ( ! class_exists( 'Tasty_Recipes' ) ) { 
        return ''; 
    } 
    $recipes = Tasty_Recipes::get_recipes_for_post( $post_id ); 
    if ( empty( $recipes ) ) { 
        return ''; 
    } 
    $recipe = array_shift( $recipes ); 
    $total_reviews = $recipe->get_total_reviews(); 
    if ( empty( $total_reviews ) ) { 
        return ''; 
    } 
    $average_rating = round( (float) $recipe->get_average_rating(), 1 ); 
    $out = '<p>'; 
    $out .= Tasty_RecipesRatings::get_rendered_rating( $average_rating ); 
    $out .= ' <span class="rating-label">'; 
    $out .= sprintf( 
        // translators: Ratings from number of reviews.
         __( '%1$s from %2$s reviews', 'tasty-recipes' ), 
        '<span class="average">' . $average_rating . '</span>', 
        '<span class="count">' . (int) $total_reviews . '</span>' ); 
    $out .= '</span>'; 
    $out .= '</p>';
     return $out;
}
Was this article helpful?

Related Articles

Need Support?

Can't find the answer you're looking for?
Contact Support