Skip to content

Instantly share code, notes, and snippets.

@ivandoric
Created April 23, 2014 15:42
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 ivandoric/11220540 to your computer and use it in GitHub Desktop.
Save ivandoric/11220540 to your computer and use it in GitHub Desktop.
wordpress: Print featured image title insted of post title
/* Add this to functions.php */
<?php
function pj_featured_img_title() {
global $post;
$pj_thumbnail_id = get_post_thumbnail_id($post->ID);
$pj_thumbnail_image = get_posts(array('p' => $pj_thumbnail_id, 'post_type' => 'attachment', 'post_status' => 'any'));
if ($pj_thumbnail_image && isset($pj_thumbnail_image[0])) {
return $pj_thumbnail_image[0]->post_title;
}
}
?>
/* Print it in theme */
<?php echo pj_featured_img_title(); ?>
/* Print it in title attribute (this example also show how to link featured image to large image) */
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
$img_title = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . pj_featured_img_title('echo=0') . '" >';
the_post_thumbnail('IMAGE-SIZE');
echo '</a>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment