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 🙂
The topic ‘Rich Snippet 'Recipe' with nested repeater fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.