Support

Account

Home Forums Front-end Issues Specific-size image from another page

Solved

Specific-size image from another page

  • Hey there,

    I am using some code to grab an image from the previous page:

    <?php
        $page_title = get_page_by_title( 'How it Works' );
        $page_image = get_field('title', $page_title->ID);
    ?>
    <img src="<?php echo $page_image; ?>"  />
    

    The code works just fine without issues. But this code returns the full size image.

    Normally, I’d do something like this to return the smaller version: <img src="<?php echo $page_image['sizes']['thumbnail']; ?>" /> but when I do that, nothing gets displayed. I have this code working elsewhere, so I know that syntax should work…my guess is because I’m adding the ID from another page the syntax is off, so my code needs to be changed or adjusted slightly…can anyone point me in the right direction?

    Thanks,
    Josh

  • From the look of your code, you have the field set to return the URL of the image.

    To do it differently you would need to alter the return value of the field to return an array, which would likely mean altering the code everywhere that this field is used.

    Instead of doing that I would get the unformatted value from ACF by using false as the 3rd argument

    
    $page_image = get_field('title', $page_title->ID, false);
    

    And then I’d get the image I want using

    
    $image = wp_get_attachment_image_src($page_image, 'the size you want here');
    ?>
    img src="<?php echo $image[0]; ?>"  />
    
  • Hey there,

    Thanks for your reply, I actually just figured this out before I read your answer…total rookie mistake. In ACF I chose Image URL instead of Image Array. I was thinking because it was a single field there would be no “array”, as there is only one item…but that literally just passed the URL through and didn’t give me all the options that the array provides.

    So, the code didn’t change:

    <?php
         $page_title = get_page_by_title( 'How it Works' );
         $page_image = get_field('title', $page_title->ID);
    ?>
         <img src="<?php echo $page_image['sizes']['thumbnail']; ?>" />

    Thanks,
    Josh

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

You must be logged in to reply to this topic.