Skip to content

Instantly share code, notes, and snippets.

@richardmtl
Forked from kraftbj/functions.php
Created January 31, 2014 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardmtl/8740558 to your computer and use it in GitHub Desktop.
Save richardmtl/8740558 to your computer and use it in GitHub Desktop.
Make homepage og:image be an image from the latest post
<?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