0

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; ?>
2

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.

0

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.