Created
January 31, 2014 18:49
-
-
Save kraftbj/8740216 to your computer and use it in GitHub Desktop.
Use image from the latest post for the og:image tag on the home page
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