Support » Plugin: Advanced Custom Fields: Extended » How to retrieve image by ID inside a group?

  • Resolved snippet24

    (@snippet24)


    So I have already know to use groups as a loop, but since here I’m not using loops it may be more correct to use the non-loop way.
    Which would be this:

    <?php
    $hero = get_field('hero');
    if( $hero ): ?>
        <div id="hero">
            <img src="<?php echo esc_url( $hero['image']['url'] ); ?>" alt="<?php echo esc_attr( $hero['image']['alt'] ); ?>" />
            <div class="content">
                <?php echo $hero['caption']; ?>
                <a href="<?php echo esc_url( $hero['link']['url'] ); ?>"><?php echo esc_html( $hero['link']['title'] ); ?></a>
            </div>
        </div>
        <style type="text/css">
            #hero {
                background-color: <?php echo esc_attr( $hero['color'] ); ?>;
            }
        </style>
    <?php endif; ?>

    Taken from the docs here:
    https://www.advancedcustomfields.com/resources/group/

    So the question is how can I specify images by ID inside a group?
    Equivalent t this to code:

    <picture>
      <?php $imagexoriginal = wp_get_attachment_image_src(2237, 'full'); ?>
      <?php $imagex815 = wp_get_attachment_image_src(2237, 'medium_large'); ?>
      <?php $imagex352 = wp_get_attachment_image_src(2237, 'medium'); ?>
    
        <source srcset="<?php echo $imagex352[0]; ?>" media="(max-width: 352px)">
        <source srcset="<?php echo $imagex815[0]; ?>" media="(max-width: 380px)">
        <source srcset="<?php echo $imagexoriginal[0]; ?>" media="(max-width: 767px)">
        <source srcset="<?php echo $imagex815[0]; ?>" media="(max-width: 815px)">   
        <source srcset="<?php echo $imagexoriginal[0]; ?>" media="(min-width: 816px)">
    
        <img class="thumbnail" srcset="<?php echo $image2xretina[0]; ?>" alt="<?php the_title(); ?>">
     </picture>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter snippet24

    (@snippet24)

    Ok I have several other issues please help!

    1. the conditional field returns nothing

    $test_de_estres = get_field('test_de_estres');
    	if( $test_de_estres ): ?>

    2. removing the if above, the title isn’t displayed, it returns “Trying to access array offset on value of type null in” :
    <h2 style="color: #555555;" class="titulo-seccion text-center"><?php echo $test_de_estres['titulo_de_seccion']; ?></h2>

    3. Full code

    <div class="container-fluid">
    	<div class="row">
    
    	<?php
    	$test_de_estres = get_field('test_de_estres');
    	if( $test_de_estres ): ?>
    
    		<div class="col mt-5 mb-n4 text-center">
    		      
    
    		      <h2 style="color: #555555;" class="titulo-seccion text-center"><?php echo $test_de_estres['titulo_de_seccion']; ?></h2>
    	    </div>
    	<?php endif; ?>
    	</div>
    </div>
    Thread Starter snippet24

    (@snippet24)

    Okey is working now.. figured it out… still have to get working the picture image part..

    • This reply was modified 2 months, 1 week ago by snippet24.
    Thread Starter snippet24

    (@snippet24)

    Ok did it.. wasn’t that difficult, in case it helps someone else here’s the working code:

    <picture>
    			  <?php $image2xretina = wp_get_attachment_image_src($test_de_estres['imagen'], 'large'); ?>
    			  <?php $image1xretina = wp_get_attachment_image_src($test_de_estres['imagen'], 'large'); ?>
    			    <source srcset="<?php echo $image2xretina[0]; ?>" media="(min-width: 1600px)">
    			    <source srcset="<?php echo $image1xretina[0]; ?> 1x, <?php echo $image2xretina[0]; ?> 2x">
    			    <!--<img class="thumbnail" srcset="<?php echo $image2xretina[0]; ?>" alt="<?php the_title(); ?>">-->
    			    <img class="thumbnail" srcset="<?php echo $image2xretina[0]; ?>" alt="<?php echo $test_de_estres['titulo_de_seccion']; ?>">
    			 </picture>
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback and sorry for the late answer. I’m glad that you found the solution to your problem. It seemed more related to custom ACF development, and not the ACF Extended plugin itself tho.

    Have a nice day and happy new year!

    Regards.

    Thread Starter snippet24

    (@snippet24)

    Hi,
    No problem 🙂

    It seemed more related to custom ACF development, and not the ACF Extended plugin itself tho.

    I know though sadly there’s no support; no responses to support topics in the ACF plugin WordPress forum 🙁
    I used in the past the this forum https://support.advancedcustomfields.com/ but my questions were unanswered. So the only option left is to create tickets that doesn’t help anybody else as are not public.

    Have a nice day and happy new year!

    You too and thanks for the help! 🙂

    • This reply was modified 2 months ago by snippet24.
    • This reply was modified 2 months ago by snippet24.
    • This reply was modified 2 months ago by snippet24.
    • This reply was modified 2 months ago by snippet24.
    • This reply was modified 2 months ago by snippet24.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.