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 have been trying a way round my latest WordPress problem all morning and I feel now is the right time to shout and ask for help.

I have been trying to create a people directory, I'm there except for the overall listing page which I have decided to be rows of 5 images followed by the persons name below each image, followed by another a row etc...

Each persons page has a set of custom fields one of which is name and another is mainimage the idea being that with the following code I can pull the image along with the name from the child pages of people and display them all on the one page with the image and name linked to the permalinkof the specific child page.

<?php 
    // Get the page's children
    $args = array( 'numberposts' => 100, 'child_of' => 35, 'post_type' => page, );
    $children = get_pages( $args );

if (!empty($children)) { 
    echo '';
    foreach($children as $child) {
        // Get the 2 meta values from the child page
        $main_image = get_post_meta($child->ID, 'main_image', true); 
        $name = get_post_meta($child->ID, 'name', true); 

        // Display the meta values
        echo '<br />';
        echo $main_image;
        echo '<br />';
        echo $name;
    }
    echo '';
}      
?> 

It works kind of, but I'm getting a number for the image as opposed something that outputs to an image. Upon checking the custom field I have found the image value is a number, looks to me like media ID.

I need to either be able to interpret the ID within the people page as an image or a way to make sure actual urls are being put into the custom field values so I can use standard img src html to output the image.

I am using Advanced Custom Fields to create the fieldsets for the people page as well as the individual person pages.

Any help would be greatly appreciated as I have tried several variations of the code above but just cannot get the number to change in to the associated image.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Use wp_get_attachment_image( $id ).

Sample code:

print wp_get_attachment_image( 
    $main_image,
    array ( 80, 80 ),
    FALSE, 
    array ( 'alt' => 'portrait' )
);
share|improve this answer
    
That works well. Thanks. But I'm getting a number 1 printed after each image and how can I specify a custom size of the images (190x220)? –  Ben Sep 22 '12 at 13:27
    
Not sure about the extra number, I cannot test right now. For custom image sizes use add_image_size(). –  toscho Sep 22 '12 at 13:36
    
Thanks again... Got it working correctly, don't quite understand what I did to stop the number displaying but it's not anymore :) –  Ben Sep 23 '12 at 18:56
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.