Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add structured data schema in array

Tags:

json

arrays

php

How do I add structured data mainEntity as an array @type question?

As you can see from my code, I need to have the main entity as an array so I can pass advanced custom fields repeater rows into the schema in an attempt to build an FAQ.

The fields that I'm trying to pass into the structured data is a very basic ACF repeater as you can see here:

<section class="faq">
    <div class="container max-width px-5">
        <div class="row">
            <div class="col-lg-6 features-title-block">
                <h2 class="mpc-header"> <?php if ( get_field('faq_header') ) : ?>
                    <?php the_field('faq_header'); ?>
                    <?php endif; ?></h2>
                    <?php if ( get_field('faq_sub_text') ) : ?>
                    <div class="pb-5"><?php the_field('faq_sub_text'); ?></div>
                <?php endif; ?>
            </div>
            <div class="col-lg-6">
                <?php if ( have_rows('faq') ) : ?>
                <div class="accordion" id="accordionExample">
                    <?php while( have_rows('faq') ) : the_row(); ?>
                    <div class="card">
                        <div class="card-header" id="heading<?php echo get_row_index(); ?>">
                            <h2 class="mb-0">
                                <button class="btn btn-link d-inline-flex" type="button" data-toggle="collapse"
                                    data-target="#collapse<?php echo get_row_index(); ?>" aria-expanded="true"
                                    aria-controls="collapse<?php echo get_row_index(); ?>">
                                    <?php the_sub_field('question'); ?>
                                </button>
                            </h2>
                        </div>
                        <div id="collapse<?php echo get_row_index(); ?>" class="collapse"
                            aria-labelledby="heading<?php echo get_row_index(); ?>" data-parent="#accordionExample">
                            <div class="card-body">
                                <?php the_sub_field('answer'); ?>
                            </div>
                        </div>
                    </div>
                    <?php endwhile; ?>
                    <?php
                    $schema = array(
                    '@context'   => "https://schema.org",
                    '@type'      => "FAQPage",
                    'mainEntity' => array()
                    );
                    global $schema;
                    if ( have_rows('faq') ) {
                        while ( have_rows('faq') ) : the_row();
                            $questions = array(
                                '@type'          => 'Question',
                                'name'           => get_sub_field('question'),
                                'acceptedAnswer' => array(
                                '@type' => "Answer",
                                'text' => get_sub_field('answer')
                                )
                                );
                                array_push($schema['mainEntity'], $questions);
                                        endwhile;
                    function generate_faq_schema ($schema) {
                    global $schema;
                    echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';
                    }
                    add_action( 'wp_footer', 'generate_faq_schema', 100 );
                    }
                    ?>
                    <?php endif; ?>
                    <!-- endif have_rows('faq'); -->
                </div>
            </div>
        </div>
    </div>
</section>
like image 677
Brad Holmes Avatar asked Jan 15 '20 20:01

Brad Holmes


People also ask

What is structured data and schema?

Structured data is a system of pairing a name with a value that helps search engines categorize and index your content. Microdata is one form of structured data that works with HTML5. Schema.org is a project that provides a particular set of agreed-upon definitions for microdata tags.

Where do I put the schema code on my website?

But for the most part, you will want to place the schema markup HTML in the footer of every page of your website. We are going to do that by clicking on Appearance, then Customize, then Widgets, and then the footer section in which we want to place the code.

How do I add a rescued column to my schema?

When Auto Loader infers the schema, a rescued data column is automatically added to your schema as _rescued_data. See the section on rescued data column and schema evolution for details. Binary file ( binaryFile) and text file formats have fixed data schemas, but also support partition column inference.

What are structured arrays?

Structured arrays are ndarrays whose datatype is a composition of simpler datatypes organized as a sequence of named fields.

How do you assign values from one structured array to another?

]) Assigns values from one structured array to another by field name. Normally in numpy >= 1.14, assignment of one structured array to another copies fields “by position”, meaning that the first field from the src is copied to the first field of the dst, and so on, regardless of field name.

How do I access and modify individual fields of a structured array?

You can access and modify individual fields of a structured array by indexing with the field name: Structured datatypes are designed to be able to mimic ‘structs’ in the C language, and share a similar memory layout.


Video Answer


2 Answers

I am not a pro in WordPress, but if you avoid global variables as much as you can, this will be good for your application. Just try the below code.

<?php
function generate_faq_schema () {
    if ( have_rows('faq') ) {
        $schema = [
            '@context'   => "https://schema.org",
            '@type'      => "FAQPage",
            'mainEntity' => array()
        ];
        while ( have_rows('faq') ) : the_row();
            $questions = [
                '@type'          => 'Question',
                'name'           => get_sub_field('question'),
                'acceptedAnswer' => [
                    '@type' => "Answer",
                    'text' => get_sub_field('answer')
                ]
            ];
            array_push($schema['mainEntity'], $questions);
        endwhile;
        echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';
    }
}
add_action( 'wp_footer', 'generate_faq_schema', 100 );

?>

Also check for fourth argument of add_action here.

like image 135
Aabir Hussain Avatar answered Oct 18 '22 09:10

Aabir Hussain


<section class="faq">
    <div class="container max-width px-5">
        <div class="row">
            <div class="col-lg-6 features-title-block">
                <h2 class="mpc-header"> <?php if ( get_field('faq_header') ) : ?>
                    <?php the_field('faq_header'); ?>
                    <?php endif; ?></h2>
                    <?php if ( get_field('faq_sub_text') ) : ?>
                    <div class="pb-5"><?php the_field('faq_sub_text'); ?></div>
                <?php endif; ?>
            </div>
            <div class="col-lg-6">
                <?php if ( have_rows('faq') ) : ?>
                <div class="accordion" id="accordionExample">
                    <?php while( have_rows('faq') ) : the_row(); ?>
                    <div class="card">
                        <div class="card-header" id="heading<?php echo get_row_index(); ?>">
                            <h2 class="mb-0">
                                <button class="btn btn-link d-inline-flex" type="button" data-toggle="collapse"
                                    data-target="#collapse<?php echo get_row_index(); ?>" aria-expanded="true"
                                    aria-controls="collapse<?php echo get_row_index(); ?>">

                                    <?php the_sub_field('question'); ?>

                                </button>
                            </h2>
                        </div>
                        <div id="collapse<?php echo get_row_index(); ?>" class="collapse"
                            aria-labelledby="heading<?php echo get_row_index(); ?>" data-parent="#accordionExample">
                            <div class="faq-content card-body">
                                <?php the_sub_field('answer'); ?>
                            </div>
                        </div>
                    </div>
                    <?php endwhile; ?>
                    <?php
                    $schema = array(
                    '@context'   => "https://schema.org",
                    '@type'      => "FAQPage",
                    'mainEntity' => array()
                    );
                    global $schema;
                    if ( have_rows('faq') ) {
                        while ( have_rows('faq') ) : the_row();
                            $questions = array(
                                '@type'          => 'Question',
                                'name'           => get_sub_field('question'),
                                'acceptedAnswer' => array(
                                '@type' => "Answer",
                                'text' => get_sub_field('answer')
                                )
                                );
                                array_push($schema['mainEntity'], $questions);
                                        endwhile;
                    function generate_faq_schema ($schema) {
                    global $schema;
                    echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';
                    }
                    add_action( 'wp_footer', 'generate_faq_schema', 100 );
                    }
                    ?>
                    <?php endif; ?>
                    <!-- endif have_rows('faq'); -->
                </div>
            </div>
        </div>
    </div>
</section>

After lots of testing, both answers received some sort of a result, but the actually working code has no errors and is pulling through to Google is above.

Please feel free to make any suggestion on making this better.

like image 27
Brad Holmes Avatar answered Oct 18 '22 10:10

Brad Holmes