我正在运行一个既有传统页面又有AMP页面的WordPress网站。这两个版本都使用GTM,但是AMP版本无法找到我的自定义变量。 AMP页面正在验证中,因此我认为如何定义自定义变量存在问题
我正在使用以下代码来定义AMP页面上的变量。有什么地方我想念的吗?要查看AMP页面的示例,请访问https://cellculturedish.com/evolution-stem-cell-culture-cell-derived-extracellular-matrices/amp/。
<body>
<!-- Google Tag Manager -->
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=GTM-xxxxxxxxx.url=SOURCE_URL" data-credentials="include">
<?php if ( is_singular() ) { ?>
<?php global $post; ?>
<?php $categories = get_the_category( $post->ID ); ?>
<?php $primary_category = $categories[0]; ?>
<?php $post_sponsorship = get_field('post_sponsorship', $post->ID); ?>
<?php $is_sponsored = $post_sponsorship['is_sponsored']; ?>
<?php $author = 'Multiple Authors'; ?>
<?php $author_options = get_field('author_options', $post->ID); ?>
<?php if ( $author_options == 'ccd' && get_field('ccd_author') ) { ?>
<?php $author_obj = get_field('ccd_author', $post->ID); ?>
<?php $author = get_userdata( $author_obj ); ?>
<?php $author = $author->data->display_name; ?>
<?php } elseif ( $author_options == 'guest_post' && count( get_field('contributing_experts', $post->ID) ) == 1 ) { ?>
<?php $author_array = get_field('contributing_experts', $post->ID); ?>
<?php $author = get_expert_title( $author_array[0] ); ?>
<?php } ?>
<script type="application/json">
{
"vars": {
"publishDate": "<?php echo get_the_date('', $post->ID); ?>",
"postCategory":"<?php echo $primary_category->name; ?>",
"isSponsored": "<?php echo $sponsored = $post_sponsorship['is_sponsored'] == true ? 'true' : 'false'; ?>",
<?php if ( $post_sponsorship['is_sponsored'] == true && $post_sponsorship['sponsor'] != null ) { ?>
"postSponsor": "<?php echo get_the_title( $post_sponsorship['sponsor'] ); ?>",
<?php } ?>
"postAuthor": "<?php echo $author; ?>"
}
}
</script>
<?php } ?>