Custom Field returning Numerical Value for Image rather than URL

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…

Read More

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.

Related posts

Leave a Reply

2 comments

  1. When using get_field() or the_field() and the Image ID is returned instead of the expected return value, this is a sign that acf_add_local_field_group() was not run. Therefore ACF is pulling the raw value from the database but has no way to determine in which format you want it.

    To resolve the issue, ensure that acf_add_local_field_group() is run from the init hook, not one of the admin_* hooks.