Adding recipe structured data to post template

Thank you Patric! I managed to find a different solution. I duplicated the single.php document from the bricks template folder and added it to my child theme. I then added the following JSON code to fetch my ACF fields. Haven’t set it up fully yet but seems to be working!

<?php
get_header();

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();

        $post_id     = get_the_ID();
        $post_type   = get_post_type();
        $bricks_data = Bricks\Helpers::get_bricks_data( $post_id, 'content' );
        $preview_id  = Bricks\Helpers::get_template_setting( 'templatePreviewPostId', $post_id );

        // Get custom field data
        $recipe_name = get_the_title(); // Use the post title for the recipe name
        $recipe_description = get_field('recipe_description');
        $recipe_ingredients = get_field('recipe_ingredients');
        $recipe_instructions = get_field('recipe_instructions');
        $prep_time = get_field('prep_time');
        $cook_time = get_field('cook_time');
        $total_time = get_field('total_time');
        $recipe_image = get_the_post_thumbnail_url($post_id, 'full'); // Get the featured image URL

        // Build JSON-LD script
        $recipe_schema = [
            "@context" => "http://schema.org/",
            "@type" => "Recipe",
            "name" => $recipe_name,
            "description" => $recipe_description,
            "image" => $recipe_image,
            "recipeIngredient" => $recipe_ingredients,
            "recipeInstructions" => $recipe_instructions,
            "prepTime" => "PT" . $prep_time . "M",
            "cookTime" => "PT" . $cook_time . "M",
            "totalTime" => "PT" . $total_time . "M",
            // Add any other fields required by the Recipe schema
        ];

        // Convert array to JSON-LD format
        $json_ld = json_encode($recipe_schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        ?>
1 Like