问题
When validating my wordpress posts using Google's Structured Data Testing Tool, I get the following error:
"Image: missing and required"
I have the official wordpress AMP plugin installed that generated the AMP pages for me. The problem is that it doesn't popular the "image" attribute for BlogPosting
.
In the plugin there is a code that I think should generate it, but it's not running anywhere:
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;
}
How can I populate the image tag for each post using this AMP WordPress plugin? I want the page to pass the Structured Data Testing Tool, so he can pass the AMP validation as well.
Update: the reason that the image is not showing is because there is not embedded image in the post. Is there a way to put a default image in case there isn't one, so it will pass the AMP/Schema validation.
回答1:
For conforming to the AMP HTML Specification, you don’t have to use Schema.org structured data.
If the Google SDTT says that a property is "missing and required", it does not mean that it’s required by AMP or Schema.org. It only means that Google won’t show one of their Google Search results features (like Rich Snippets) for your page.
For example, there’s the Top Stories with AMP feature: a carousel that links to AMP pages, showing an image from each page. That’s why Google requires the Schema.org image
property for this feature (for articles). But it’s perfectly fine not to provide an image for your article, the only thing that "happens" is that your page doesn’t get a chance to appear in that Top Stories carousel.
You could of course populate $post_image_meta
with a default image (e.g., a placeholder) if is_array( $post_image_src )
is false, but that’s very likely not a good idea: the image wouldn’t be relevant, so users searching on Google Search wouldn’t find it useful, so Google Search would have an interest not to show your result to their users. (But if and in which way that’s actually the case is an SEO issue, which belongs to Webmasters SE.)
回答2:
Default image for old posts without images
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;
}
From amp plugin support. I've checked and it works. Images should be at least 696 pixels wide.
来源:https://stackoverflow.com/questions/35710385/image-missing-and-required-wordpress-amp-structure-doesnt-add-image-attribute