0

I'm using Advanced Custom Fields plugin to generate schema data in json-ld format. I'm using the repeater for breadcrumbs and it's working wonderfully, however to get valid markup I need to remove the comma from the last repeater/breadcrumb item. Here's my code:

<?php if( have_rows('breadcrumb') ): ?>
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [

  <?php while( have_rows('breadcrumb') ): the_row();
    // vars
    $position = get_sub_field('position');
    $name = get_sub_field('name');
    $url = get_sub_field('url');
    ?>

    {
      "@type": "ListItem",
      "position": <?php echo $position; ?>,
      "item": {
        "@id": "<?php echo $url; ?>",
        "name": "<?php echo $name; ?>"
        }
    }, // <-- This comma needs to go on last repeater item

  <?php endwhile; ?>

  ]}
</script>
<?php endif; ?>

The result if there were just two breadcrumb items (minus the comma on the last item) should be:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [
  {
    "@type": "ListItem",
    "position": 1,
    "item": {
      "@id": "http://example.com/",
      "name": "Movies"
      }
    }, // <-- Comma because there's another list item to follow this one
  {
    "@type": "ListItem",
    "position": 2,
    "item": {
      "@id": "http://example.com/avatar",
      "name": "Avatar"
      }
    } // <-- No comma as this is the last list item
  ]
}
</script>
1

Mixing up code of different languages is not a good practice. Further, building a JSON format as kind of a template is not necessary. You can build your data structure and then format it to a valid JSON string using json_encode():

 <?php
if ( have_rows( 'breadcrumb' ) ) {

    $json_data = [
        "@context"        => "http://schema.org",
        "@type"           => "BreadcrumbList",
        "itemListElement" => []
    ];

    while ( have_rows( 'breadcrumb' ) ) {
        the_row();

        // vars
        $position = get_sub_field( 'position' );
        $name     = get_sub_field( 'name' );
        $url      = get_sub_field( 'url' );

        $json_data[ 'itemListElement' ][] = [
            "@type"    => "ListItem",
            "position" => $position,
            "item"     => [
                "@id"  => $url,
                "name" => $name
            ]
        ];
    }

    $json_string = json_encode( $json_data );
    $html_script = <<<HTML
<script type="application/json">{$json_string}</script>
HTML;

    echo $html_script;
}

I assume that this code is in some kind of template file, as you directly print the results. You should consider to separate your business logic (building your data structure) from the presentation/formatting of your data, as this makes the code more readable and maintainable.

| improve this answer | |
  • 1
    Thanks David, this appears to have done exactly what I needed and also you've given me something to think about so I will study your code further and hopefully put it to good use elsewhere. – Glen Aug 4 '16 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.