简体   繁体   English

Yoast SEO (WordPress Plugin) - 手动获取插件生成的数据

[英]Yoast SEO (WordPress Plugin) - Get plugin generated data manually

I wanted to get the Yoast SEO generated data manually, see example code below我想手动获取 Yoast SEO 生成的数据,请参见下面的示例代码

This data is being generated by Yoast and automatically add it inside the head tag .此数据由 Yoast 生成并自动将其添加到head 标签内。

<!-- This site is optimized with the Yoast SEO plugin v4.2.1 - https://yoast.com/wordpress/plugins/seo/ -->
<meta name="description" content="bla bla bla"/>
<meta name="robots" content="noodp"/>
<link rel="canonical" href="http://example.localhost.com/" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="bla bla bla" />
<meta property="og:url" content="http://example.localhost.com/" />
<meta property="og:site_name" content="Example.com" />
<meta property="og:image" content="http://example.com.au/wp-content/uploads/2016/11/example.png" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:description" content="bla bla bla." />
<meta name="twitter:title" content="bla bla bla" />
<meta name="twitter:image" content="http://example.com.au/wp-content/uploads/2016/11/example.png" />
<script type='application/ld+json'>{"@context":"http:\/\/schema.org","@type":"WebSite","@id":"#website","url":"http:\/\/example.localhost.com\/","name":"Example","potentialAction":{"@type":"SearchAction","target":"http:\/\/example.localhost.com\/?s={search_term_string}","query-input":"required name=search_term_string"}}</script>
<!-- / Yoast SEO plugin. -->

I do not want to use wp_head();我不想使用wp_head(); because it also generates other scripts, styles and whatever plugin or code you have in your wordpress website.因为它还会生成其他脚本,styles 以及您在 wordpress 网站中拥有的任何插件或代码。

I do not need all those codes.我不需要所有这些代码。 I only want to get the Yoast SEO generated code as shown above.我只想获取 Yoast SEO 生成的代码,如上所示。 any ideas how could i do that?任何想法我怎么能这样做?

You can get the yoast meta in any page by get_post_meta() , 您可以通过get_post_meta()在任何页面中获取yoast元数据,

get the meta values by following, 通过以下方式获取元值,

echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true); 
echo get_post_meta(get_the_ID(), '_yoast_wpseo_title', true); 

Check the post_meta table to get all the values related to each page/post. 检查post_meta表以获取与每个页面/帖子相关的所有值。

Use this below: 使用以下内容:

do_action( 'wpseo_head' );

It will print all SEO header in your head. 它会打印出你头脑中的所有SEO标题。 No need to add through post meta. 无需通过post meta添加。

It's four years later, so things have changed here.四年过去了,所以这里发生了变化。 The data can now be retrieved via API in a structured way: Example:现在可以通过 API 以结构化方式检索数据:示例:

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

See: https://developer.yoast.com/customization/apis/surfaces-api/请参阅: https://developer.yoast.com/customization/apis/surfaces-api/

Also via REST a access is possible: https://developer.yoast.com/customization/apis/rest-api也可以通过 REST 访问: https://developer.yoast.com/customization/apis/rest-api

May that sometimes helps.可能这有时会有所帮助。 To get the meta at all (unstruktured), the both other ways are sufficient.要完全获得元数据(非结构化),其他两种方式都足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM