WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic Featured Image
[resolved] could you please explain simply how to loop through images. (2 posts)

  1. creativemanner
    Member
    Posted 7 months ago #

    trying to loop through all the featured images.

    <?php
       while ( have_posts() ) : the_post();
    $attachments = dfi_get_featured_images($post->ID);
    foreach($attachments as $attachment) {
    $medium_img_url = dfi_get_image_url( $attachment['attachment_id'], 'medium' );
    ?>
    <li>
    <a class="gall-img" href="<?php the_permalink() ?>" style="margin-bottom: 0;">
    <img src="<?php echo $medium_img_url; ?>" alt="<?php echo dfi_get_image_alt_by_id($attachment['attachment_id']); ?>" />
    </a>
    </li>
    <?php
    } endwhile;
    ?>

    http://wordpress.org/plugins/dynamic-featured-image/

  2. Ankit Pokhrel
    Member
    Plugin Author

    Posted 7 months ago #

    Hello creativemanner,

    Thank you for using the plugin. I have tried to explain the code in comments.

    <?php
       //if there is any post
       while ( have_posts() ) : the_post(); //get the psot
    		$attachments = dfi_get_featured_images($post->ID); //dfi_get_featured_images will fetch all DFI images from your post
    		foreach($attachments as $attachment) { //Now loop through your returned image array
    			$medium_img_url = dfi_get_image_url( $attachment['attachment_id'], 'medium' ); //dfi_get_image_url will return medium sized url of your image
    			?>
    			<li>
    				<!-- Display your image. the_permalink() will get the link to your post. dfi_get_image_alt_by_id will get the alt field of your image. -->
    				<a class="gall-img" href="<?php the_permalink() ?>" style="margin-bottom: 0;"> <img src="<?php echo $medium_img_url; ?>" alt="<?php echo dfi_get_image_alt_by_id($attachment['attachment_id']); ?>" /> </a>
    			</li>
    <?php
       } endwhile;
    ?>

    Thanks,
    Ankit

Reply

You must log in to post.

About this Plugin

About this Topic