admin管理员组文章数量:1203198
I wanted to use the following function to override the Yoast meta description:
add_filter('wpseo_metadesc','custom_meta');
function custom_meta( $desc ){
if (/* do your test here to check template or any other values*/) {
$desc = "Change the description";
}
return $desc;
}
However, Yoast SEO support recommends the Metadata API.
From this page, I understand I can use the wpseo_metadesc
filter to adjust the Meta_Description_Presenter
, but I am unsure how it is done; I am not a programmer.
I'd love some assistance to create some functions.php
code that will grab the first 160 characters of the content if the meta description hasn't been set already.
Help appreciated. Steve
I wanted to use the following function to override the Yoast meta description:
add_filter('wpseo_metadesc','custom_meta');
function custom_meta( $desc ){
if (/* do your test here to check template or any other values*/) {
$desc = "Change the description";
}
return $desc;
}
However, Yoast SEO support recommends the Metadata API.
From this page, I understand I can use the wpseo_metadesc
filter to adjust the Meta_Description_Presenter
, but I am unsure how it is done; I am not a programmer.
I'd love some assistance to create some functions.php
code that will grab the first 160 characters of the content if the meta description hasn't been set already.
Help appreciated. Steve
Share Improve this question asked Mar 10, 2022 at 2:17 SteveSteve 1,75719 gold badges66 silver badges115 bronze badges1 Answer
Reset to default 2 +100I think you're on the right track here. You can use the wpseo_metadesc
filter to alter the meta description any way you want. Check out below code, maybe it'll help you with your function.
add_filter( 'wpseo_metadesc', 'my_custom_meta_description' );
function my_custom_meta_description($description) {
if ( !$description || empty($description) ) {
global $post;
$content = get_the_content($post->ID);
if ( $content && !empty($content) ) {
$description = substr($content, 0, 160);
}
}
return $description;
}
I haven't tested the function so you may need to customize it for your need.
本文标签: Yoast Metadata API to adjustoverride the meta description
版权声明:本文标题:Yoast Metadata API to adjustoverride the meta description 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738664801a2105637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论