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.
Use
wp_get_attachment_image( $id )
.Sample code:
When using
get_field()
orthe_field()
and the Image ID is returned instead of the expected return value, this is a sign thatacf_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 theinit
hook, not one of theadmin_*
hooks.