-
-
Save kevinwhoffman/4218c502c1859096af25d1f663b76173 to your computer and use it in GitHub Desktop.
For ACF users, creates an easy to use function that wraps your image in HTML5 <figure> adds <figcaption>. Also adds Schema markup in JSON LD. Parameters are 1. ACF field name, 2. Size, 3. Figure CSS class, 4. Figcaption CSS class.
This file contains hidden or 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
function fsc_figure( $image, $size, $imageclass, $captionclass ){ | |
/** | |
* Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. | |
* | |
* @param string $image The ACF field name (i.e. 'your_photo_name'). | |
* @param string $size Thumbnail size (i.e. 'Thumbnail', 'Medium', 'Large') | |
* @param string $imageclass The Figure class you want to use (ex: 'my-figure') | |
* @param string $captionclass The Figcaption class you want to use (ex: 'caption-blue') | |
*/ | |
$image = get_field( $image ); | |
$size = $size; | |
$thumb = $image['sizes'][ $size ]; | |
if( !empty($image) ): ?> | |
<figure class="<?php echo $imageclass; ?>"> | |
<?php echo wp_get_attachment_image( $image['ID'], $size, false, array( 'alt' => $image['alt'] ) ); ?> | |
<figcaption class="<?php echo $captionclass; ?>"> | |
<?php echo $image[ 'caption' ]; ?> | |
</figcaption> | |
</figure> | |
<script type="application/ld+json"> | |
{ | |
"@context": "http://schema.org", | |
"@type": "ImageObject", | |
"author": "<?php the_author(); ?>", | |
"contentUrl": "<?php echo $thumb; ?>", | |
"datePublished": "<?php echo $image[ 'created_timestamp' ]; ?>", | |
"description": "<?php echo $image[ 'alt' ]; ?>", | |
"name": "<?php echo $image[ 'title' ]; ?>", | |
"copyright": "<?php echo $image[ 'copyright' ]; ?>" | |
} | |
</script> | |
<?php endif; | |
} |
@theMikeD Thanks, I was very quickly turning this around to show a fellow AWP member how to integrate responsive images in their code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some observations:
Line 12: You assume that ACF is set to return the image object. You should indicate that because this will fail if ACF is set to return the image ID.
Line 13: redundant
Line 14: no error checking on the requested size
Lines 18 and 22, and 30-40: you pass through or use values directly without sanitizing first
Hope that helps!