Last active
March 25, 2023 10:16
-
-
Save seredniy/978365c219e28d35f82a07f0d70bd3e8 to your computer and use it in GitHub Desktop.
Wordpress image helper with alt, title, classes and kama thumbnail
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 | |
/** | |
* Display image with all attributes | |
* Function uses Kama Thumbnail plugin to crope images | |
* | |
* @param int $id attachment id | |
* @param string $kama_args string of arguments for kama_thumbnail | |
* @param string $classes list of classes separated by space | |
* @param string $alt custom alt for image | |
* @param string $title custom title for image | |
*/ | |
function scf_kama_image( $id, $kama_args = "w=100 &h=100", $classes = "", $alt = "", $title = "" ) { | |
$image_id = ( $id ) ? $id : get_the_ID(); | |
$image = get_post( $image_id ); | |
$image_url = kama_thumb_src( $kama_args, $image_id ); | |
$image_alt = ( $alt ) ? $alt : get_post_meta( $image_id, '_wp_attachment_image_alt', true ); | |
$image_title = ( $title ) ? $title : $image->post_title; | |
$image_classes = ( $classes ) ? $classes : "scf_image"; | |
echo "<img src='$image_url' class='$image_classes' alt='$image_alt' title='$image_title'>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment