如何知道yoast seo图像是否已上传,并在wordpress中为og:image添加条件
在WordPress的社交分享中,我正在使用yoast seo插件。现在根据客户要求,我需要为og:image设置如下条件如何知道yoast seo图像是否已上传,并在wordpress中为og:image添加条件,wordpress,share,yoast,Wordpress,Share,Yoast,在WordPress的社交分享中,我正在使用yoast seo插件。现在根据客户要求,我需要为og:image设置如下条件 如果图像添加到post/page的yoast元框中,则显示该图像 如果yoast seo中没有图像,请检查帖子/页面中的特色图像 若在yoast seo中并没有图像,那个么特征图像将从设置中显示一些图像。 我如何知道图片是否上传到yoast meta box中用于特定页面并满足上述要求? 我尝试了以下方法,但它只替换了yoast meta box中的图像 以下解决方案对我有
以下解决方案对我有效
add_action( 'wp_head', 'insert_fb_in_head', 5 );
function insert_fb_in_head() {
global $post;
if( ! has_post_thumbnail( $post->ID ) ) { //the post does not have featured image, use a default image
$default_image = get_field( 'social_share_image', 'options' ); //replace this with a default image on your server or an image in your media library
echo '<meta property="og:image" content="' . $default_image . '"/>';
} else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
}
add_action('wp_head','insert_fb_in_head',5);
函数插入头()中的函数{
全球$员额;
如果(!has_post_缩略图($post->ID)){//文章没有特色图像,请使用默认图像
$default_image=get_字段('social_share_image','options');//将其替换为服务器上的默认图像或媒体库中的图像
回声';
}否则{
$thumbnail\u src=wp\u get\u attachment\u image\u src(get\u post\u thumbnail\u id($post->id),'medium');
回声';
}
}
我没有测试真正的塞纳里奥。但希望它能为你工作。因此,您可以通过以下代码进行检查:add_filter('wpseo_opengraph_image','ag_yoast_seo_fb_share_images',10,1);函数ag_yoast_seo_fb_share_images($img){global$post;if($img&&!empty($img)){return$img;}else if(is_page()| is|seo|is_single(){$image=get_post_缩略图_url($post->ID,'full');if(empty($image)){return get_字段('social_share_图像,'options')}选项)}else{return return get}get}('social_share_image','options');};
add_action( 'wp_head', 'insert_fb_in_head', 5 );
function insert_fb_in_head() {
global $post;
if( ! has_post_thumbnail( $post->ID ) ) { //the post does not have featured image, use a default image
$default_image = get_field( 'social_share_image', 'options' ); //replace this with a default image on your server or an image in your media library
echo '<meta property="og:image" content="' . $default_image . '"/>';
} else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
}