Yoast SEO 开发者文档

title: "Article" post_status: publish comment_status: open taxonomy: category: - yoast-developer post_tag: - Pieces - Schema - Features


import YoastSchemaExample from '../../../../src/components/YoastSchemaExample';

描述 WebPage 上的一个 Article。 如果满足所需条件,可转换为更具体的类型(例如 NewsArticle)。

触发器

应在所有支持 作者身份 且拥有作者的文章类型上输出。

必需属性

有效的 Article 必须包含以下属性。

失败场景

如果任何必填字段缺失或无效,则不应输出该节点。

可选属性

以下属性应在可用且有效时添加:

条件属性

仅在满足特定条件时才应输出的可选属性。

若存在版权信息

如果存在评论

如果允许/启用评论功能

Examples

Minimum criteria

{{ "@context": "https://schema.org", "@graph": [ { "@type": "Article", "@id": "https://www.example.com/#/schema/Article/abc123", "headline": "Example article headline", "description": "Example article description", "isPartOf": { "@id": "https://www.example.com/blog/example-article/" }, "mainEntityOfPage": { "@id": "https://www.example.com/blog/example-article/" }, "datePublished": "2019-07-10T08:08:40+00:00", "dateModified": "2019-07-10T08:43:03+00:00", "author": { "@id": "https://www.example.com/#/schema/Person/abc123" }, "publisher": { "@id": "https://www.example.com/#/schema/Organization/1" }, "image": { "@id": "https://www.example.com/uploads/example-image.jpg" } } ] }}

Extended criteria

{{ "@context": "https://schema.org", "@graph": [ { "@type": "Article", "@id": "https://www.example.com/#/schema/Article/abc123", "headline": "Example article headline", "description": "Example article description", "isPartOf": { "@id": "https://www.example.com/blog/example-article/" }, "mainEntityOfPage": { "@id": "https://www.example.com/blog/example-article/" }, "datePublished": "2019-07-10T08:08:40+00:00", "dateModified": "2019-07-10T08:43:03+00:00", "commentCount": 6, "articleSection": ["cats","dogs","cake"], "inLanguage": "en-US", "author": { "@id": "https://www.example.com/#/schema/Person/abc123" }, "publisher": { "@id": "https://www.example.com/#/schema/Organization/1" }, "image": [ { "@id": "https://www.example.com/uploads/example-image.jpg" }, { "@id": "https://www.example.com/uploads/example-image-2.jpg" } ], "video": [ { "@id": "https://www.example.com/#/schema/VideoObject/abc123" }, { "@id": "https://www.example.com/#/schema/VideoObject/def456" } ], "potentialAction": [ { "@type": "CommentAction", "name": "Comment" "target": [ "https://www.example.com/blog/example-article/#comment" ] } ] } ] }}

WordPress API:修改文章 Schema 输出 {#api}

如需修改 Yoast SEO 输出的 Article schema,可使用 wpseo_schema_article 过滤器。示例如下:

add_filter( 'wpseo_schema_article', 'change_article_to_social_posting' );

/**
 * 修改文章 Schema 数据的 @type 属性。
 *
 * @param array $data Schema.org 文章数据数组。
 *
 * @return array Schema.org 文章数据数组。
 */
function change_article_to_social_posting( $data ) {
    $data['@type'] = 'SocialMediaPosting';

    return $data;
}

在自定义文章类型上输出文章架构

默认情况下,Yoast SEO 不会在自定义文章类型上输出文章架构。以下代码可改变此行为:

/**
 * 将 'book' 添加到文章类型列表中,使书籍获得文章架构。
 *
 * @param  mixed $post_types 当前文章类型列表。
 *
 * @return array Yoast SEO 为其渲染文章架构的文章类型数组。
 */
function article_schema_for_books( $post_types ) {
    $post_types[] = 'book';
    return $post_types;
}

add_filter( 'wpseo_schema_article_post_types', 'article_schema_for_books' );

请注意,要使文章类型能够输出 Article 架构,该文章类型需要支持作者功能。 对于给定的文章类型 book,您可以通过添加以下代码轻松实现:

add_post_type_support( 'book', 'author' );

目前这不适用于 page 文章类型。我们正在修复此问题。

Add images to your Article Schema

If you want to add an image to an object programmatically, you can do it as follows. Note that we use the YoastSEO surface to get both the canonical from the meta surface as the helpers surface to access the schema->image functionality.

add_filter( 'wpseo_schema_article', 'example_change_article' );

/**
 * Adds images to Article Schema data.
 *
 * @param array $data Schema.org Article data array.
 *
 * @return array Schema.org Article data array.
 */
function example_change_article( $data ) {
    // This is the attachment ID for our image.
    $attachment_id = 12345;

    // We're going to create a graph piece for our image. Every graph piece always needs a Schema ID, so it can
    // be referenced by other graph pieces, best practice is to base that on the canonical adding an ID that's
    // always going to be unique.
    $schema_id     = YoastSEO()->meta->for_current_page()->canonical . '#/schema/image/' . $attachment_id;                 
    $data['image'] = new WPSEO_Schema_Image( $schema_id );

    return $data;
}

Instead of YoastSEO()->helpers->schema->image->generate_from_attachment_id() you can also use YoastSEO()->helpers->schema->image->generate_from_url() which takes, as you've guessed, a URL as input.

To make more changes to our Schema output, see the Yoast SEO Schema API.