Support

Account

Home Forums ACF PRO Rich Snippet 'Recipe' with nested repeater fields

Unread

Rich Snippet 'Recipe' with nested repeater fields

  • Hi,
    I’m working on adding an LD-JSON Rich Snippet for Google SEO on some webpages containing recipes. On some of those recipes, steps are divided into sections. In that case, Google says I should output a script looking like this :

    "recipeInstructions": [
      {
        "@type": "HowToSection",
        "name": "Prepare the Crust",
        "itemListElement": [
          {
            "@type": "HowToStep",
            "text": "Heat oven to 425°F."
          }, {
            "@type": "HowToStep",
            "text": "Place 1 pie crust in ungreased 9-inch glass pie plate."
          }
        ]
      }, {
        "@type": "HowToSection",
        "name": "Add the Filling",
        "itemListElement": [
          {
            "@type": "HowToStep",
            "text": "In large bowl, gently mix filling ingredients; spoon into
            crust-lined pie plate."
          }, {
            "@type": "HowToStep",
            "text": "Top with second crust."
          }
        ]
      }
    ]

    When there is no sections, it should look like this :

    "recipeInstructions": [
      {
        "@type": "HowToStep",
        "text": "Heat oven to 425°F."
      }, {
        "@type": "HowToStep",
        "text": "Place 1 pie crust in ungreased 9-inch glass pie plate, pressing
        firmly against side and bottom."
      }, {
        "@type": "HowToStep",
        "text": "In large bowl, gently mix filling ingredients; spoon into
        crust-lined pie plate."
      }
    ]

    Here is my setup : I’ve got an array $schema in which I’m pushing the section/steps arrays.

    $schema = array(
        '@context'  => "http://schema.org",
        '@type'     => "Recipe",
        'name'      => $name,
    );
    
    if (have_rows('myrecipe')) {
        $schema['recipeInstructions'] = array();
        
        while (have_rows('myrecipe')) : the_row();
          if( have_rows('step') ):
            while( have_rows('step') ): the_row();
    
            $step = get_sub_field('step_title');
    
            // If sections
            if($step){
              $howtosection = array(
                '@type'     => 'HowToSection',
                'name' => $step,
              );
              array_push($schema['recipeInstructions'], $howtosection);
    
              if( have_rows('instructions') ):
                while( have_rows('instructions') ): the_row();
    
                $howtosection['itemListElement'] = array();
                $instruction = get_sub_field('instruction', false, false);
    
                $howtostep = array(
                    '@type'     => 'HowToStep',
                    'text' => $instruction,
                );
                
                array_push($howtosection['itemListElement'], $howtostep);
    
                endwhile;
              endif;
            }
    
            // If no section
            else {
    
              if( have_rows('instructions') ):
                while( have_rows('instructions') ): the_row();
    
                $instruction = get_sub_field('instruction', false, false);
    
                $howtostep = array(
                    '@type'     => 'HowToStep',
                    'text' => $instruction,
                );
    
                array_push($schema['recipeInstructions'], $howtostep);
    
                endwhile;
              endif;
    
            }
    
            endwhile;
          endif;
        endwhile;
    }
    

    However, I don’t know hot to push the “itemListElement” with the “HowToStep” arrays inside the “HowToSection” array ! Too much nested repeaters for me 😡

    Does anyone know how I could make this work ?

    Thanks 🙂

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.