Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 } ?>
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
thank you both for you help! –  JonnyPlow Aug 7 '11 at 2:26
add comment
foreach ($images as $n=>$att){
    //some code
    if (in_array($n, array(0,3,8)){
         //do this
    } else {
         //do something entirely different
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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