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.

how do I match an image title from the media library to a variable then echo that image? this is my current code...

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

if ($attachment->post_title == $programme) {

    echo the_attachment_link($post->ID, false);
}
?>
share|improve this question

3 Answers 3

$attachments holds array of posts, you are missing something like:

$attachment = $attachments[0];

or

foreach( $attachments as $attachment )

Update

Nope, like this:

foreach( $attachments as $attachment )
    if ($attachment->post_title == $programme) {

        echo the_attachment_link($post->ID, false);
        break;
    }
share|improve this answer
    
hmmm, nearly now I get Missing Attachment? –  erichmond Mar 5 '11 at 12:00

Hmm, still no joy, I get nothing:

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

if ($attachment->post_title == $programme) {
    foreach ( $attachments as $attachment ) {
        echo the_attachment_link($post->ID, false);
    }
}
?>
share|improve this answer
1  
Updated my answer. Please use comments or edit your original answer for updates (unless it's final working and so answer-worthy solution). –  Rarst Mar 4 '11 at 18:18

Sorted for anyone else wanting to do this the code is:

<?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_link($attachment->ID, 'thumbnail', false, false);
            break;
        }

}
?>
share|improve this answer

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.