-
-
Save richardmtl/8740558 to your computer and use it in GitHub Desktop.
Make homepage og:image be an image from the latest post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function fb_home_image( $tags ) { | |
if ( is_home() ) { | |
$latest_post = get_posts("post_type=post&numberposts=1"); | |
$latest_post = $latest_cpt[0]->ID; | |
if ( class_exists( 'Jetpack_PostImages' ) ) { | |
$post_images = Jetpack_PostImages::get_images( $latest_post, array( 'width' => 200, 'height' => 200 ) ); | |
if ( $post_images && !is_wp_error( $post_images ) ) { | |
$image = array(); | |
foreach ( (array) $post_images as $post_image ) { | |
$image[] = $post_image['src']; | |
} | |
} | |
} | |
if ( empty( $image ) ) | |
$image = array(); | |
else if ( !is_array( $image ) ) | |
$image = array( $image ); | |
// Remove the default blank image added by Jetpack | |
unset( $tags['og:image'] ); | |
$tags['og:image'] = $image; | |
} | |
return $tags; | |
} | |
add_filter( 'jetpack_open_graph_tags', 'fb_home_image' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment