Force og:image for custom image for different custom post type

#188889
  • Resolved Andrea Vergottini
    Rank Math free

    Hi,
    is it possible to force an og:image image (other than the featured image) on a custom post type?

    Specifically in the custom post type I have images and I would like to set one of these.
    Maybe through a special hook it is possible?

    Regards

Viewing 1 replies (of 1 total)
  • Hello,

    Thanks for contacting us.

    You can modify and use the code given below in your theme’s/child theme’s functions.php file to set a custom og:image for your CPT:

    add_filter( "rank_math/opengraph/facebook/image", function( $attachment_url ) {
    	if(is_singular()){
    		global $post;
    	  $post_slug = $post->post_name;
    		$attachment_url = 'url of the image';
    	}
    	return $attachment_url;
    });
    add_filter( "rank_math/opengraph/twitter/image", function( $attachment_url ) {
    	if(is_singular()) {
    		global $post;
    	  $post_slug = $post->post_name;
    		$attachment_url = 'url of the image';
    	}
    	return $attachment_url;
    });

    Hope this helps. Let us know if you need any further assistance.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 1 replies (of 1 total)

The ticket ‘Force og:image for custom image for different custom post type’ is closed to new replies.