0

I want to specify for the images of my post - if is number x, output this, else if is number y output this instead, etc. I'm trying make specific treatments to my 1st, 4th, 9th images for that post. This code below is the code i'm using to just simply output all of my images for the post. Any ideas?

<?php $images = get_post_meta($post->ID, 'rw_postpage_images');  ?>
<?php foreach ($images as $att) {
$src = wp_get_attachment_image_src($att, 'full');
$src = $src[0]; 
$image_path =  thumbGen($src,80,80,"crop=1&halign=center&valign=center&return=1");
?>
<div class="post_item">
<div class="small">
<img src="<?php echo $image_path; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="80"  height="80"/>
</div>
</div>
<?php } ?>
0

set a counter variable outside the foreach, check its value in each iteration and output corresponding markup, then increment it at the end for the next iteration.

<?php
$images = get_post_meta($post->ID, 'rw_postpage_images');
// set a counter
$image_counter = 1;
foreach ($images as $att) :
    $src = wp_get_attachment_image_src($att, 'full');
    $src = $src[0]; 
    $image_path =  thumbGen($src,80,80,"crop=1&halign=center&valign=center&return=1");
    // check the counter
    if( $imagecounter == 1 ) :
        // output markup for first image
    elseif( $imagecounter == 4 ):
        // output markup for fourth image
    elseif( $imagecounter == 9 ):
        // output markup for ninth image
    else :
        // markup for all the others
    ?>
        <div class="post_item">
            <div class="small">
                <img src="<?php echo $image_path; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="80"  height="80"/>
            </div>
        </div>
    <?php
    endif;
    // increment the counter
    $imagecounter++;
endforeach;
?>

I put comments in there where markup would go to keep things shorter. just put whatever markup you want output there for that particular position similar to the last else: block.

0
0
foreach ($images as $n=>$att){
    //some code
    if (in_array($n, array(0,3,8)){
         //do this
    } else {
         //do something entirely different
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.