Skip to content

Instantly share code, notes, and snippets.

@derekshirk
Created July 20, 2016 22:14
Show Gist options
  • Save derekshirk/f2c1bb844a5bc98a038cc40fdb5f559a to your computer and use it in GitHub Desktop.
Save derekshirk/f2c1bb844a5bc98a038cc40fdb5f559a to your computer and use it in GitHub Desktop.
Output Inline SVGs from ACF image field
<?php
/* ————————————————————————— */
/* ACF Inline SVGs
/* ————————————————————————— */
$repeater = 'repeater_field_name'
if ( have_rows( $repeater ) ): ?>
<section class="content-main">
<div class="container"> <?php
while ( have_rows( $repeater ) ) : the_row();
$icon = get_sub_field( 'icon' );
if ( !empty( $icon ) ):
$url = $icon['url'] ;
$ext = pathinfo( $url, PATHINFO_EXTENSION ); ?>
<div class="icons"> <?php
if ( $ext == 'svg' ):
echo file_get_contents( $url ) ;
// Non SVG Fallback
else: ?>
<img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>"> <?php
endif; ?>
</div> <?php
endif;
endwhile; ?>
</div>
</section> <?php
endif; ?>
@marcelo2605
Copy link

echo file_get_contents( get_site_url().$url ) ; to avoid failed to open stream error.

@sagive
Copy link

sagive commented Apr 24, 2020

echo file_get_contents( get_site_url().$url ) ; to avoid failed to open stream error.

Why? The url from the image field is a full none relative URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment