Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

insert a function after post title in loop?

  • Hello,
    I’m trying to add recipe rating stars in from the WPRM plugin after the title in the loop. Right now, I’m using the standard loop and recipes are on a custom post type. Sticking the shortcode to display star ratings into a loop element does not work as it does not get the ratings for the recipe for the specific post.

    I found this snippet:

    /*try to get the recipe stars in recipe archives*/
    /**
     * 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 . '"]' );
    	}
    
    }

    To add to functions.php, then you’re supposed to be able to call be_star_rating() in a template to get the recipe ratings. Is there any way to do this in a GP element applied to just recipe archives, recipe-category archives and recipe-tag archives?

    I’ve tried a few things that just don’t work. Or would I actually have to customize the standard loop template?
    Thanks.

  • You will need to use the render_block() filter to run the be_star_rating() function.

    Try this:

    1. Add a headline block to the query loop, just add static text, star rating placeholder, add a CSS class to it, eg. star-rating.
    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    2. then add this PHP code:

    
    add_filter( 'render_block_generateblocks/headline', function( $block_content, $block ) {
        // Check if it's not in the admin and the block contains the class 'star-rating'
        if ( 
            ! is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'star-rating' ) !== false 
        ) {
            // Capture the output of be_star_rating()
            ob_start();
            be_star_rating();
            $star_rating = ob_get_clean();
    
            // Return the star rating if it exists, otherwise return nothing
            return $star_rating ?: '';
        }
    
        return $block_content;
    }, 10, 2 );
  • Thanks, that worked great!

  • You are welcome   🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.