3 回答

TA貢獻1852條經驗 獲得超7個贊
我不是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的第四個參數。

TA貢獻1815條經驗 獲得超6個贊
<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>
經過大量的測試,兩個答案都收到了某種結果,但實際工作的代碼沒有錯誤,并且正在拉到Google上面。
請隨時提出任何建議,使它變得更好。

TA貢獻1804條經驗 獲得超8個贊
我已經測試了輸出您正在尋找的確切格式,以下是我的片段作為示例:
<?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);
?>
- 3 回答
- 0 關注
- 162 瀏覽
添加回答
舉報