Skip to content

Instantly share code, notes, and snippets.

@yoanmarchal
Created September 24, 2015 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoanmarchal/a55f306134c2c076df8e to your computer and use it in GitHub Desktop.
Save yoanmarchal/a55f306134c2c076df8e to your computer and use it in GitHub Desktop.
get img from page / article (wordpress)
<?php
$id = get_the_id();
$attachments = get_attached_media( 'image', $id );
if(empty($attachments)){
?>
<div class="slider" data-url=["http:\/\/lescaladaulemosi.fr\/wp-content\/uploads\/2015\/07\/home-compressor.jpg"]>
</div>
<?php
} else {
$attachments = get_children(array('post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'));
$array =[];
foreach($attachments as $att_id => $attachment) {
$full_img_url = wp_get_attachment_url($attachment->ID);
// Your Code here
$array[] = $full_img_url;
}
$encodedImg = json_encode($array);
?>
<script>
var imgs = <?php echo json_encode($encodedImg) ?>;
</script>
<div class="slider" data-url=<?php echo $encodedImg ?>>
</div>
<?php
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment