Support

Account

Home Forums General Issues Display Image Field From Another Page

Solved

Display Image Field From Another Page

  • Hi,

    So I’m looking to create a page which displays pages that use a specific template (I’ve managed to get this in list form). But I also want to display a image that uses a custom field from these pages.

    So essentially I want to display an image (named ‘Profile_image’) on another page.

    How would I go about this?

    Thanks
    Jake

  • It works just like an image field on the same page, but you need to supply the post ID to get the value from. Since you have a list of posts to display I’m assuming you also have the post ID. something similar to this:

    get_field('image', $post->ID);

    See “Get value from other places” on this page https://www.advancedcustomfields.com/resources/get_field/

  • Hi John,

    Thanks a lot for your help, I’m still not having much luck though unfortunately.

    if(!empty($pages))
    {
    foreach($pages as $page){
    echo $page->post_title.'<br />’;
    echo get_field(‘Profile_image’, $post->ID);

    }

    This is the code I have at the minute. The post_title displays perfectly but still not got anything for the image.

    Any ideas?

    Thanks
    Jake

  • 
    if(!empty($pages)) {
      foreach($pages as $page){
        echo $page->post_title.'<br />’;
        echo get_field(‘Profile_image’, $page->ID); // change here
      }
    }
    
  • Still no luck unfortunately John.

    I even changed the field in the piece of code to a text field just to make sure it was nothing to do with the it being an image I was calling for but that didn’t work either.

  • I’m still having issues with this. Any ideas?

  • Your previous reply must have gotten lost.

    The code that you posted above, except for one typo, looks like it should be working. My last reply corrected what I thought to be problems.

    
    if(!empty($pages)) {
      foreach($pages as $page){
        echo $page->post_title.'<br />';
        echo get_field('profile_image', $page->ID);
      }
    }
    

    The only reasons this should not work is that profile_image is not a field on $page->ID or that the value returned is not an image URL because you are getting something for $page->title

  • Thanks for your reply John.

    Would it help if I put the code in that I use for the image in my other template (where it’s trying to pull it from)?

    Thanks
    Jake

  • It might, there must be some difference that I have not been able to see.

  • Cool, thanks. Here it is:

    <?php 
    $image = get_field('profile_image');
    
    if( !empty($image) ): ?>
    
    	<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    
    <?php endif; ?>
  • try putting the same code used there inside your loop

    
    if(!empty($pages)) {
      foreach($pages as $page){
        echo $page->post_title.'<br />';
        $image = get_field('profile_image');
        if (!empty($image)) {
          ?><img src="<?php 
               echo $image['url']; ?>" alt="<?php 
               echo $image['alt']; ?>" /><?php
        }
      }
    }
    
  • Thank you very much! That’s worked! I want to put each result inside a div. Where would I put the div tags in that code?

    Thanks again
    Jake

  • 
    if(!empty($pages)) {
      foreach($pages as $page){
        ?><div><?php 
        echo $page->post_title.'<br />';
        $image = get_field('profile_image');
        if (!empty($image)) {
          ?><img src="<?php 
               echo $image['url']; ?>" alt="<?php 
               echo $image['alt']; ?>" /><?php
        }
        ?></div><?php 
      }
    }
    
  • Perfect! Thank you very much for your help John, it’s really appreciated!

    Thanks again
    Jake

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

The topic ‘Display Image Field From Another Page’ is closed to new replies.