Skip to content

Instantly share code, notes, and snippets.

@sabotawsh
Last active August 17, 2022 15:32
Show Gist options
  • Save sabotawsh/15ad8056e87cc8baa4f174d273921f89 to your computer and use it in GitHub Desktop.
Save sabotawsh/15ad8056e87cc8baa4f174d273921f89 to your computer and use it in GitHub Desktop.
When using Advanced Custom Field plugin for WordPress, this allows you to target custom image sizes
<?php
$attachment_id = get_field('image');
$size = "square";
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img alt="Image of <?php the_title(); ?>" src="<?php echo $image[0]; ?>">
// IF GROUP
<?php
$attachment_id = get_sub_field('image');
$size = "square";
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img alt="Image of <?php the_title(); ?>" src="<?php echo $image[0]; ?>">
// IMAGE ARRAY
<?php
$image = get_sub_field('image');
$image_url = $image['sizes']['slider'];
$size = "slider";
?>
<img src="<?php echo $image_url; ?>" alt="<?php echo esc_attr($image['alt']); ?>">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment