Custom opengraph image with Yoast SEO

Yoast SEO plugin automatically add the featured image of posts as an image opengraph meta tag. If you want to override or customize this setting, you can make use of filter.

In this case, we needed to display a custom featured image of a taxonomy which is assigned to the tax with a custom field added by ACF plugin.


function my_own_og_function() {

	if(is_tax('tax_name')){
		$tax_name = get_term_by('slug', get_query_var('tax_name'), 'tax_name');
		$img = get_field('image', 'tax_name_'.$tax_name->term_id);
		if($img){
			$GLOBALS['wpseo_og']->image_output( $img['sizes']['large'] );
		}
	}
    
}
add_action( 'wpseo_opengraph', 'my_own_og_function', 29 );

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *