Support

Account

Home Forums Backend Issues (wp-admin) Images URL & ID !

Solved

Images URL & ID !

  • Hi everyone !
    I try to place an image on custom post type, and I want my be surrounded by a <a> tag with href=”image-url”.
    Except that my <img /> need an ID for custom size on front end( thumbnail ), and I need URL for <a href>.

    The image field type leaves me only one choice, Object, URL or ID, I need URL AND ID.

    At this time, I found one solution but not practical :
    I’ve got 2 fields with 2 images field type ( the same ), one with URL and one with ID. And I show it like this :

    <figure>;
    <?php
    $attachment_id = get_field('image');
    $size = "img-thumb";
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    // url = $image[0];
    // width = $image[1];
    // height = $image[2];
    ?>
    <a href=<?php the_field('url'); ?> rel="lightbox">
    <span class="ititle"><?php the_field('titre'); ?></span>
    <img src=<?php echo $image[0]; ?> />
    </a>
    </figure>

    Have you got a better solution for me ? My client is looking for simplicity I can’t see myself asking him to upload the image 2 times whenever.

    Thanks for your help

  • I might be misunderstanding but the image object should provide you with both the URL and the ID.

    Be sure to set the upload to “Image Object” in the ACF field editor. Then something like this should work:

    $img = get_field('image');
    <a href=<?php echo $img["url"] ?> rel="lightbox">
      <span class="ititle"><?php the_field('titre'); ?></span>
      <img src=<?php echo $img["sizes"]["large"]; ?> />
    </a>

    Replace [“large”] with which ever size you want. Do a print_r($img) to see what’s available.

  • Yes, that’s what I want ! 🙂

    Else, I’ve tested another way :

    <?php	$attachment_id = get_field('image');
    $size = "img-thumb"; // (thumbnail, medium, large, full or custom size)
    $size2 = "large";
    $image = wp_get_attachment_image_src( $attachment_id, $size );
    $image2 = wp_get_attachment_image_src( $attachment_id, $size2 );
    // url = $image[0];
    // width = $image[1];
    // height = $image[2];
    ?>
    <a href=<?php echo $image2[0]; ?> class="ai-thumb" rel="lightbox">
    <span class="ititle"><?php the_field('titre'); ?></span>
    <img  src=<?php echo $image[0]; ?> />
    </a>

    And it works too 🙂

    Thanks a lot !

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

The topic ‘Images URL & ID !’ is closed to new replies.