0

Im trying to display a list of child pages under $parent_id. This is done in a page template inside the loop. You will see that Im trying to also get a advanced custom field value which in this case is a image url. I have everything working except the image url, Currently i'm getting the value "80" in the image source. Any ideas?

<?php
$parent_id = $posts[0]->ID;
$args=array(
  'post_parent' => $parent_id,
  'post_type' => 'page',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'List of Posts';
  while ($my_query->have_posts()) : $my_query->the_post(); ?>

<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

<?php if ( get_post_meta( get_the_ID(), 'story_detail_story_image', true ) ) : ?>
<a href="<?php the_permalink() ?>" rel="bookmark">
    <img class="thumb" src="<?php echo get_post_meta( get_the_ID(), 'story_detail_story_image', true ); ?>" alt="<?php the_title(); ?>" />
</a>
<?php endif; ?>
<?php   endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
  • 1
    you have set return value on your image field to image ID. the documentation shows examples for fetching images for the different return values available to the image field. – Milo Feb 9 '15 at 22:35
0

From what I see there, I'd expect that your problem is the meta value in story_detail_story_image is storing an image id.

Try replacing the following

<?php if ( get_post_meta( get_the_ID(), 'story_detail_story_image', true ) ) : ?>
<a href="<?php the_permalink() ?>" rel="bookmark">
    <img class="thumb" src="<?php echo get_post_meta( get_the_ID(), 'story_detail_story_image', true ); ?>" alt="<?php the_title(); ?>" />
</a>
<?php endif; ?>

With the following

<?php
$image_id = get_post_meta( get_the_ID(), 'story_detail_story_image', true );
if ( ! is_int( $image_id ) ) {
    echo "<p>Image ID not an integer. It is:</p><pre>"; var_dump( $image_id ); echo "</pre>";
} else if ( $image_id ) {

    # The next line tries to grab an image with that id in thumbnail size
    $image_data = wp_get_attachment_image_src( $image_id, 'thumbnail' );
    if ( ! is_array( $image_data ) ) {
        echo "<p>Image data not an array! It is:</p><pre>"; var_dump( $image_data ); echo "</pre>";
    } else if ( is_array( $image_data ) ) {
        # The next line grabs the url, which is stored in the first element of the array
        $image_url = $image_data[0];

        # the next three lines just grab post information
        $post_id = get_the_ID();
        $post_title = esc_attr( get_the_title() );
        $post_link = get_permalink();

        # The following displays the link. I just prefer heredoc syntax but you should be able to change it back if wanted
        echo <<<HTML
<a href="{$post_link}" rel="bookmark">
    <img class="thumb" src="{$image_url}" alt="{$post_title}" />
</a>
HTML;

    }
}
?>

That should grab the image id, use it to get the url, and then draw your link.

(revised to output what is wrong).

  • i modified the echo "<li><a href=".$post_link."><img src=".$image_url."></a></li>"; but this now doesnt return anything. – balardi Feb 9 '15 at 22:46
  • Adjusted to echo the value of variables that are not what they are expected to be so that something should be output to diagnose. – Privateer Feb 9 '15 at 22:49
  • Result: Image data not an array! It is: boolean false – balardi Feb 9 '15 at 22:50
  • Should be fixed now. The 3rd parameter to get_post_meta is to tell if it is a single entry or not ... I was thinking of get_option's extra parameter to specify a default. – Privateer Feb 9 '15 at 22:53

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.