如何在数组中添加结构化数据架构

如何将结构化数据主实体添加为数组@type问题?


从我的代码中可以看出,我需要将主实体作为数组,以便将高级自定义字段转发器行传递到架构中,以尝试构建常见问题解答。


我尝试传递到结构化数据中的字段是一个非常基本的ACF中继器,您可以在此处看到:


<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>



梦里花落0921
浏览 140回答 3
3回答

慕姐4208626

我不是WordPress的专业人士,但是如果您尽可能避免全局变量,这将对您的应用程序有好处。只需尝试以下代码。<?phpfunction generate_faq_schema () {&nbsp; &nbsp; if ( have_rows('faq') ) {&nbsp; &nbsp; &nbsp; &nbsp; $schema = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@context'&nbsp; &nbsp;=> "https://schema.org",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type'&nbsp; &nbsp; &nbsp; => "FAQPage",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mainEntity' => array()&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; while ( have_rows('faq') ) : the_row();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $questions = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'Question',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> get_sub_field('question'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'acceptedAnswer' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type' => "Answer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text' => get_sub_field('answer')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($schema['mainEntity'], $questions);&nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; &nbsp; &nbsp; echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';&nbsp; &nbsp; }}add_action( 'wp_footer', 'generate_faq_schema', 100 );?>还要在此处检查add_action的第四个参数。

红糖糍粑

<section class="faq">&nbsp; &nbsp; <div class="container max-width px-5">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-6 features-title-block">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2 class="mpc-header"> <?php if ( get_field('faq_header') ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_field('faq_header'); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?></h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ( get_field('faq_sub_text') ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="pb-5"><?php the_field('faq_sub_text'); ?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ( have_rows('faq') ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="accordion" id="accordionExample">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php while( have_rows('faq') ) : the_row(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-header" id="heading<?php echo get_row_index(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2 class="mb-0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-link d-inline-flex" type="button" data-toggle="collapse"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data-target="#collapse<?php echo get_row_index(); ?>" aria-expanded="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aria-controls="collapse<?php echo get_row_index(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_sub_field('question'); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="collapse<?php echo get_row_index(); ?>" class="collapse"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aria-labelledby="heading<?php echo get_row_index(); ?>" data-parent="#accordionExample">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="faq-content card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_sub_field('answer'); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $schema = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@context'&nbsp; &nbsp;=> "https://schema.org",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type'&nbsp; &nbsp; &nbsp; => "FAQPage",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mainEntity' => array()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $schema;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( have_rows('faq') ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( have_rows('faq') ) : the_row();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $questions = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'Question',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> get_sub_field('question'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'acceptedAnswer' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type' => "Answer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text' => get_sub_field('answer')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($schema['mainEntity'], $questions);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function generate_faq_schema ($schema) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $schema;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_action( 'wp_footer', 'generate_faq_schema', 100 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- endif have_rows('faq'); -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></section>经过大量的测试,两个答案都收到了某种结果,但实际工作的代码没有错误,并且正在拉到Google上面。请随时提出任何建议,使它变得更好。

胡说叔叔

我已经测试了输出您正在寻找的确切格式,以下是我的片段作为示例:<?php&nbsp; &nbsp; global $schema;&nbsp; &nbsp; $schema = array(&nbsp; &nbsp; &nbsp; &nbsp; '@context' => "https://schema.org",&nbsp; &nbsp; &nbsp; &nbsp; '@type' => "FAQPage",&nbsp; &nbsp; &nbsp; &nbsp; 'mainEntity' => array()&nbsp; &nbsp; );&nbsp; &nbsp; while (have_posts()) {&nbsp; &nbsp; &nbsp; &nbsp; the_post();&nbsp; &nbsp; &nbsp; &nbsp; get_template_part('template-parts/post-formats/post', get_post_format());&nbsp; &nbsp; &nbsp; &nbsp; array_push($schema['mainEntity'], array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '@type' => get_post_format(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title' => get_the_title()&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; }&nbsp; &nbsp; function generate_faq_schema()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; global $schema;&nbsp; &nbsp; &nbsp; &nbsp; echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';&nbsp; &nbsp; }&nbsp; &nbsp; add_action('wp_footer', 'generate_faq_schema', 100);?>
打开App,查看更多内容
随时随地看视频慕课网APP