0

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);
}
?>
1

$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;
    }
  • hmmm, nearly now I get Missing Attachment? – erichmond Mar 5 '11 at 12:00
-1

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);
    }
}
?>
  • 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
-1

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;
        }

}
?>

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.