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.

Struggling with this code, the conditions work, the only way I can get the wp_get_attachment_url is to loop through it but then this outputs as many images from the media library and I only want one?

  <div class="tvimage">
  <?php
    if ($imgurlbase != '' ) { ?>

    <img src="<?php bloginfo('template_url'); ?>/timthumb.php?src=<?php echo $imgurlbase . $imgfromtv; ?>&h=62&w=75" alt="<?php echo $programme; ?>">

            <?php } elseif ($imgurlbase == '' ) { ?>

            <?php
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => null,
        ); 
    $attachments = get_posts( $args );


                foreach ( $attachments as $attachment ) {

                    if ($attachment->post_title == $programme) {
                            echo wp_get_attachment_url($attachment->ID);
                        }
                    else {
                    echo '<img src="';
                    echo bloginfo('template_url');
                    echo '/images/nontvimg.jpg" alt="no tv image" />';
                    }

                }


    ?>



            <?php } ?>


</div>
share|improve this question
    
Why are you using the template URL if the image is in the media library? Uploaded items are stored in the uploads directory, which is typically at wp-content/uploads/.. Additionally -1 in the numberposts arg is setting that query to unlimited, drop the minus(to set to 1). –  t31os Mar 5 '11 at 17:24
    
because the first image is a custom field and is also run through a script, I need to run through all the images to find a match for $programme but I only want to output one –  erichmond Mar 5 '11 at 17:35
    
Like i said, change the -1 to a 1 and you'll get only one image back, not mountains (minus one means no limit). –  t31os Mar 5 '11 at 17:44
    
@erichmond: I think you need to provide a bit more context to what you want to achieve. What do you mean by you only want one? You're clearly generating output for every iteration of your loop. Also, what's in $programme? And...do you maybe only want the images attached to the current post that match $programme? It's just not quite clear what you're trying to achieve. –  wyrfel Mar 5 '11 at 17:58
    
@t31os: No, he can't do that, he's trying to find the attachment by its title...unless there's a query arg for that? –  wyrfel Mar 5 '11 at 18:00

2 Answers 2

up vote 2 down vote accepted

If you want to exit the loop after you find an attachment, use break.

 // Define $programme and pull up $attachments as above
 $attachment_url = '';
 foreach ( $attachments as $attachment ) {
     if ($attachment->post_title == $programme) {
         $attachment_url = wp_get_attachment_url($attachment->ID);
         break;
     }
 }

 if ( $attachment_url ) {
     echo $attachment_url;
 } else {
     echo '/images/nontvimg.jpg';
 }
share|improve this answer

Sorted this is the new code:

<?php
            if ($imgurlbase != '' ) { ?>

            <img src="<?php bloginfo('template_url'); ?>/timthumb.php?src=<?php echo $imgurlbase . $imgfromtv; ?>&h=62&w=75" alt="<?php echo $programme; ?>">

            <?php } elseif ($imgurlbase == '' ) { ?>

                <img src="<?php bloginfo('template_url'); ?>/timthumb.php?src=<?php
                $args = array(
                    'post_type' => 'attachment',
                    'numberposts' => -1,
                    'post_title' => $programme
                    ); 
                $attachments = get_posts( $args );

                $found = 0;
                foreach ( $attachments as $attachment ) {
                   if ($attachment->post_title == $programme) {
                      echo wp_get_attachment_url($attachment->ID);
                      ++$found;
                   }
                }
                if (!$found) {
                   echo '/images/nontvimg.jpg';
                }

                ?>&h=62&w=75">


            <?php } ?>
share|improve this answer
2  
How is $programme defined? And please, stop posting answers as updates, this is not how Stackoverflow/StackExchange sites operate, unless you're providing the solution/answer to your own question. Please add any updates or new code samples to the original question. –  t31os Mar 6 '11 at 0:09

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.