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'm using the plugin advanced custom fields to create custom fields to create a relationship between two pages.

The code below is looking at the relationship field and grabbing the page title and company logo of the linked page.

The only problem is that instead of outputting the image url (so it displays the image), it's outputting the attachment ID. How can I convert the attachment ID into a url so the image displays?

        <?php foreach(get_field('connections') as $relationship): ?>
            <img src="<?php echo get_post_meta($relationship->ID, 'company_logo', true ); ?>" height="57" width="250" />        
            <a href="<?php echo get_permalink($relationship->ID); ?>"><?php echo get_the_title($relationship->ID) ?></a>
            <br />
        <?php endforeach; ?>
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You need the function wp_get_attachment_image_src() see http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

In your code it would be something like:

<?php foreach(get_field('connections') as $relationship): ?>
    <?php $img = wp_get_attachment_image_src( get_post_meta($relationship->ID, 'company_logo', true ), 'thumbnail' ); ?>
    <img src="<?php echo $img[0]; ?>"  width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />        
    <a href="<?php echo get_permalink($relationship->ID); ?>"><?php echo get_the_title($relationship->ID) ?></a>
    <br />
<?php endforeach; ?>

You can change 'thumbnail' to your desired size.

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.