Skip to content

Instantly share code, notes, and snippets.

@seredniy
Last active March 25, 2023 10:16
Show Gist options
  • Save seredniy/978365c219e28d35f82a07f0d70bd3e8 to your computer and use it in GitHub Desktop.
Save seredniy/978365c219e28d35f82a07f0d70bd3e8 to your computer and use it in GitHub Desktop.
Wordpress image helper with alt, title, classes and kama thumbnail
<?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