问题描述
酵母改变了改变og:image的方式,并删除了wpSEO_opengraph
函数并将其替换为
wpSEO_frontend_presenters
,现在我想更改og:image的值,我不明白的是如何替换og:image的值而不是添加第二个og:image,下面的代码仅添加在底部添加新的og:image,而不是替换原始og:image的值(请参见屏幕截图和代码)
use Yoast\WP\SEO\Presenters\Abstract_Indexable_Presenter;
class My_Presenter extends Abstract_Indexable_Presenter {
public function present() {
return '<Meta property="og:image" content="//d1id9ov24n1lx4.cloudfront.net/wp-content/uploads/2019/05/21155632/eview_home_hero.jpg" />';
}
public function get() {
// return $this->presentation->open_graph_image === 'nl_NL' ) ? 'Dutch' : 'Not dutch';
}
}
function add_my_presenter( $presenters ) {
$presenters[] = new My_Presenter();
return $presenters;
}
add_filter( 'wpSEO_frontend_presenters','add_my_presenter' );
我看到了这个GitHub链接并阅读了它:https://github.com/Yoast/wordpress-seo/issues/14698 但是我不知道如何使用replace_vars,我什至不确定这是否是更改og的正确方法:图像值
解决方法
尝试以下代码。它在我的Wordpress中正常运行,但是目前还不确定。
add_action( 'wpseo_opengraph','change_yoast_seo_og_meta_value' );
function change_yoast_seo_og_meta_value() {
add_filter( 'wpseo_opengraph_image','change_image_of_og' );
}
function change_image_of_og( $image ) {
if( $image == '' ) {
$image = 'image path';
}
return $image;
}