【问题标题】:How to add structured data schema in array如何在数组中添加结构化数据模式
【发布时间】:2020-05-02 15:42:40
【问题描述】:

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

从我的代码中可以看出,我需要将主实体作为一个数组,这样我就可以将高级自定义字段重复行传递到架构中,以尝试构建一个 FAQ

我试图传递到结构化数据中的字段是一个非常基本的 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>
                        <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>

【问题讨论】:

  • 嗨@BradHolmes,您能粘贴当前表格中所需的数组和数组吗?很容易理解您的要求
  • 嗨@AabirHussain 我已经用acf中继器信息更新了问题
  • 您是否收到任何错误或警告?据我了解,您可以显示FAQ但无法生成Schema对吗?
  • 为什么需要全局 $schema 的另一件事;您可以在generate_faq_schema 内移动条件和循环,而不是在函数外计算。我觉得问题属于这里。如果您认为我的方向正确,我可以粘贴此代码的答案。
  • yes faq 显示正常,它只是错误的架构

标签: php arrays json


【解决方案1】:

我不是 WordPress 的专业人士,但如果您尽可能避免使用全局变量,这对您的应用程序会有好处。试试下面的代码。

<?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 );

?>

同时检查 add_action here 的第四个参数。

【讨论】:

    【解决方案2】:
    <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>
    

    经过大量测试,两个答案都收到了某种结果,但实际工作的代码没有错误,并且正在拉到谷歌上面。

    请随时提出任何改进建议。

    【讨论】:

    • 你能解释一下为什么你需要全局$schema;这对我将来会有帮助,谢谢
    【解决方案3】:

    我已经测试输出你正在寻找的确切格式,这里以我的 sn-ps 为例:

    <?php
        global $schema;
    
        $schema = array(
            '@context' => "https://schema.org",
            '@type' => "FAQPage",
            'mainEntity' => array()
        );
    
        while (have_posts()) {
            the_post();
            get_template_part('template-parts/post-formats/post', get_post_format());
            array_push($schema['mainEntity'], array(
                '@type' => get_post_format(),
                'title' => get_the_title()
            ));
        }
    
        function generate_faq_schema()
        {
            global $schema;
            echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
        }
    
        add_action('wp_footer', 'generate_faq_schema', 100);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多