Created
April 23, 2014 15:42
-
-
Save ivandoric/11220540 to your computer and use it in GitHub Desktop.
wordpress: Print featured image title insted of post title
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
/* 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