图片缺失且必需 - Wordpress AMP 结构未添加图片属性

Image missing and required - Wordpress AMP Structure doesn't add Image attribute

使用 Google 的结构化数据测试工具验证我的 wordpress posts 时,出现以下错误:

"Image: missing and required"

我安装了为我生成 AMP 页面的官方 wordpress AMP 插件。问题是它不流行 BlogPosting 的 "image" 属性。

在插件中有一个我认为应该生成它的代码,但它不是 运行 任何地方:

private function get_post_image_metadata() {
    $post_image_meta = null;
    $post_image_id = false;

    if ( has_post_thumbnail( $this->ID ) ) {
        $post_image_id = get_post_thumbnail_id( $this->ID );
    } else {
        $attached_image_ids = get_posts( array(
            'post_parent' => $this->ID,
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'posts_per_page' => 1,
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'fields' => 'ids',
            'suppress_filters' => false,
        ) );

        if ( ! empty( $attached_image_ids ) ) {
            $post_image_id = array_shift( $attached_image_ids );
        }
    }

    if ( ! $post_image_id ) {
        return false;
    }

    $post_image_src = wp_get_attachment_image_src( $post_image_id, 'full' );

    if ( is_array( $post_image_src ) ) {
        $post_image_meta = array(
            '@type' => 'ImageObject',
            'url' => $post_image_src[0],
            'width' => $post_image_src[1],
            'height' => $post_image_src[2],
        );
    }

    return $post_image_meta;
}

如何使用此 AMP WordPress 插件为每个 post 填充图像标签?我想让页面通过结构化数据测试工具,这样他也能通过AMP验证。

更新: 图片没有显示的原因是post中没有嵌入图片。有没有办法放置默认图像以防万一没有默认图像,这样它将通过 AMP/Schema 验证。

为了符合 AMP HTML Specification,您不必使用 Schema.org 结构化数据。

如果 Google SDTT 说 属性 是 "missing and required",这并不意味着 AMP 或 Schema.org 需要它。 Google 不会为您的页面显示他们的 Google 搜索结果功能之一(如丰富网页摘要)。

例如,此功能(文章)有 Top Stories with AMP feature: a carousel that links to AMP pages, showing an image from each page. That’s why Google requires Schema.org image 属性。但是不为您的文章提供图片是完全可以的,唯一的问题是 "happens" 是您的页面没有机会出现在热门故事轮播中。

如果 is_array( $post_image_src ) 为 false,您当然可以使用默认图像(例如占位符)填充 $post_image_meta,但这很可能不是一个好主意:图像不相关,因此在 Google 搜索上搜索的用户不会发现它有用,因此 Google 搜索有兴趣不向他们的用户显示您的结果。 (但如果实际情况如何以及实际情况如何是SEO问题,属于Webmasters SE。)

没有图片的旧帖子的默认图片

add_filter( 'amp_post_template_metadata', 'bbm_amp_modify_json_metadata', 10, 2 );
function bbm_amp_modify_json_metadata( $metadata, $post ) {
    if (!array_key_exists('image', $metadata)) {
    $metadata['image'] = array(
            '@type' => 'ImageObject',
            'url' => get_template_directory_uri() . '/images/default.png',
            'height' => 512,
            'width' => 1024,
        );
    }
    return $metadata;
}

来自 amp plugin support. I've checked and it works. Images should be at least 696 pixels wide.