Created
September 24, 2015 10:16
-
-
Save yoanmarchal/a55f306134c2c076df8e to your computer and use it in GitHub Desktop.
get img from page / article (wordpress)
This file contains 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
<?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