Support

Account

Home Forums General Issues Get image loaded

Solved

Get image loaded

  • Hej,

    i am the second coder in our team, the leader got ski accident and is in hospital right now. We need a project closed, and i have no clue.

    We need a second image loaded, which is provided via ACF, and i dont know, how to include or replace this in the following code.

    foreach ( $posts as $post ) {
       if (has_post_thumbnail($post->ID)) {
       $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
       $image_src=wp_get_attachment_image_src($post_thumbnail_id,'post-thumb');
       $homepage_secs.='<div class="thumb-container"><div class="thumb"><img class="image" src="'.$image_src[0].'" /></div></div>';
    					}
    			}

    Can someone help.

    Thanks,
    Mike.

  • Hi @cosgrove,

    To start you should probably use get_post_meta($post->id) and print the results to screen in order to see what the custom field that has the image info is actually called.

    Once you know that, you can use either the_field('image-field-name') to print the field directly, or get_field('image-field-name') to retrieve the value as a variable.

  • Hi @cosgrove

    1. Create a new image field
    2. Match it’s settings to the already existing image field
    3. Read over the above code. It is very self explanitory
    4. Duplicate the necessary code to show the 2nd image

    Thanks
    E

  • Thanks guys so far for your help.

    Im totally with it, to use get_post_meta($post->id), but i still have no idea where exactly put the_field('image-field-name') or get_field('image-field-name').

    I tried so many things, and i remember once, i saw some code of the image in the code, but i cant really remember the solution, its like to miss the forest for the trees.

    Mike

  • <?php
    
    $postMeta = get_post_meta($post->id);
    print_r($postMeta);
    
    ?>

    That should give you a print of all the custom fields attached to the post in a 'field_name' => 'field_value' array. This is just temporary code to find out the name of the field that holds the image information.

    Once you have identified the ‘field_name’ that is matched with your image ‘field_value’, do this:

    <?php
    
    foreach ( $posts as $post ) {
       if (has_post_thumbnail($post->ID)) {
        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
        $image_src=wp_get_attachment_image_src($post_thumbnail_id,'post-thumb');
        $homepage_secs.='<div class="thumb-container"><div class="thumb"><img class="image" src="'.$image_src[0].'" /></div></div>';
       }
      $second_image = get_field('field_name', $post->ID);
      // do whatever you need to with the second image
    
    ?>

    The difference between get_field() and the_field() is just that the_field() prints the field value to the screen immediately, whereas get_field() allows you to assign the result to a variable.

  • Thanks, i think im on the way, still not there, but almost.

    The code looks like this for the moment:

    foreach ( $posts as $post ) {
        $post_back_id = the_field('back_image', $udt_project->ID);
        $back_image_src=wp_get_attachment_image_src($post_back_id,'back-thumb');
        $homepage_secs.='<div class="back-thumb-container"><div class="back-thumb"><img class="image" src="'.$back_image_src[0].'" /></div></div>';
    }

    but, in the website source code, it looks like this:

    http://localhost/test/wp-content/uploads/2013/09/test.jpg
    <div class="back-thumb-container">
    <div class="back-thumb">
    <img class="image" alt="123456789" src="">
    </div>
    </div>

    I have found the path to the second image, but its not in between the src-tag.

    Any ideas?
    Thanks.

  • That’s because you’re using ‘the_field()’ instead of ‘get_field()’. As I said above, ‘the_field()’ prints the value to the screen immediately.

  • Thanks for your help, everything is working fine.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Get image loaded’ is closed to new replies.