跳到主要内容

Surfaces Api

在 Yoast SEO 14.0 中,我们引入了一种将 Yoast SEO 集成到您代码中的正式方法。我们添加了一个称为表面的功能,名为 YoastSEO()。这个表面让您能轻松访问 Yoast SEO 提供的众多功能。

轻松获取当前页面的 SEO 数据

当前页面的所有 SEO 数据都可以通过我们的 surface 轻松访问。

例如,以下代码将页面标题作为变量提供:

$title = YoastSEO()->meta->for_current_page()->title;

此代码直接输出页面的元描述:

echo YoastSEO()->meta->for_current_page()->description;

此代码输出当前页面的预计阅读时间。

echo (string) YoastSEO()->meta->for_current_page()->estimated_reading_time_minutes, " minutes";

current_page surface 暴露了我们拥有的关于当前页面的所有数据,它们的工作方式都相同;这是一个很长的列表:

VariableTypeDescription
canonicalstringThe canonical URL for the current page.
descriptionstringThe meta description for the current page, if set.
titlestringThe SEO title for the current page.
idstringThe requested object ID.
site_namestringThe site name from the Yoast SEO settings.
wordpress_site_namestringThe site name from the WordPress settings.
site_urlstringThe main URL for the site.
company_namestringThe company name from the Knowledge Graph settings.
company_logo_idintThe attachment ID for the company logo.
site_user_idintIf the site represents a 'person', this is the ID of the accompanying user profile.
site_representsstringWhether the site represents a 'person' or a 'company'.
site_represents_referencearrayfalse
breadcrumbs_enabledboolWhether breadcrumbs are enabled or not.
schema_page_typestringThe Schema page type.
main_schema_idstringSchema ID that points to the main Schema thing on the page, usually the webpage or article Schema piece.
page_typestringThe Schema page type.
meta_descriptionstringThe meta description for the current page, if set.
robotsarrayAn array of the robots values set for the current page.
googlebotarrayThe meta robots values we specifically output for Googlebot on this page.
rel_nextstringThe next page in the series, if any.
rel_prevstringThe previous page in the series, if any.
open_graph_enabledboolWhether OpenGraph is enabled on this site.
open_graph_publisherstringThe OpenGraph publisher reference.
open_graph_typestringThe og:type.
open_graph_titlestringThe og:title.
open_graph_descriptionstringThe og:description.
open_graph_imagesarrayThe array of images we have for this page.
open_graph_urlstringThe og:url.
open_graph_site_namestringThe og:site_name.
open_graph_article_publisherstringThe article:publisher value.
open_graph_article_authorstringThe article:author value.
open_graph_article_published_timestringThe article:published_time value.
open_graph_article_modified_timestringThe article:modified_time value.
open_graph_localestringThe og:locale for the current page.
schemaarrayThe entire Schema array for the current page.
twitter_cardstringThe X card type for the current page.
twitter_titlestringThe X card title for the current page.
twitter_descriptionstringThe X card description for the current page.
twitter_imagestringThe X card image for the current page.
twitter_creatorstringThe X card author for the current page.
twitter_sitestringThe X card site reference for the current page.
sourcearrayThe source object for most of this page data.
breadcrumbsarrayThe breadcrumbs array for the current page.
estimated_reading_time_minutesintThe estimated reading time in minutes for the content.
post_authorstringThe name of the post author

无论你需要的是OpenGraph描述还是robots数组,这里都已涵盖。请习惯打开你最喜欢的IDE,输入YoastSEO()->meta->for_current_page()->,然后查看类型提示以获取你所需的精确数据片段。

已弃用的属性

变量名类型描述弃用版本
open_graph_fb_app_idstringFacebook 应用 ID。Yoast SEO 15.5 (2020年12月)

对于其他页面

获取任何页面的数据,其工作方式与获取当前页面数据几乎完全相同。您只需要提供一个 ID 或一个 URL。

例如,获取 ID 为 2 的文章的规范 URL 值。

YoastSEO()->meta->for_post( 2 )->canonical;

例如,获取 URL 为 https://www.example.com/example-page/ 的页面的标题:

YoastSEO()->meta->for_url( 'https://www.example.com/example-page/' )->title;

注意:如果某个 URL 在我们的 Indexables 表中不存在,则此方法将返回 false

访问我们的辅助函数

有时您需要的不仅仅是页面的原始 SEO 数据。例如,您可能需要知道当前文章类型是否应该被索引。我们的 post_type 辅助函数可以解决这个问题:

YoastSEO()->helpers->post_type->is_indexable( get_post_type() );

这将返回一个简单的 布尔值

如果您想要获取可索引的文章类型列表,应该使用:

$public_post_types = YoastSEO()->helpers->post_type->get_public_post_types();

同样的方法也适用于分类法:

YoastSEO()->helpers->taxonomy->is_indexable( 'category' );

我们提供了相当多的这类辅助函数,并非所有函数对您都同样有用。但请在您的 IDE 中查看我们提供了哪些函数,这同样是一个相当长的列表!