• Resolved Dvd709

    (@dvd709)


    I have multiple loops on my page and they’re all supposed to show images in a different div each time. Now I’m using get_post to fetch the ID from the post(upload in the cms) and this works perfectly on the first div, but when using the same method even when giving them different names get_post for some reason fetches “Hello world” the very first ID in wordpress and gives it to my images as a result I get empty src height and widths.

    Why does this happen? I reset postdata after every query.

    This is my code:

    <?php if( have_rows('gallery_project') ):
                    while( have_rows('gallery_project') ): the_row(); 
    
                    // vars
                    $image = get_sub_field('afbeelding');
                    $content = get_sub_field('caption');
                    $post_ID = get_post($image);
                    $afbeelding = image_downsize( $post_ID->ID, 'een3e');
                    $alt_img = $post_ID->post_title;
                    /*
                        $afbeelding[0] = src
                        $afbeelding[1] = width
                        $afbeelding[2] = height
                        $afbeelding[3] = resized bool
                    */?>
    
        <div class="col_4">
            <div class="thumbnail-cont">
                <img src="<?php echo $afbeelding[0]; ?>" width="<?php echo $afbeelding[1]; ?>" alt="<?php echo $alt_img; ?> "/>
                <?php //echo $content; ?>
            </div>
        </div>
                    <?php endwhile; wp_reset_postdata(); endif; ?>

    I use the same code 3x but change the names for instance: $image2 or $image 3.

    Thanks in advance!

    [Moderator Note: No bumping. If it’s that urgent after just 13 minutes(!), consider hiring someone instead.]

Viewing 3 replies - 1 through 3 (of 3 total)
  • What typical value would be assigned to the variable $image here:

    $image = get_sub_field('afbeelding');

    It’s worth pointing out that get_sub_field() is not a WordPress native function, it’s part of Advanced Custom Fields and as far as I am aware, only available as a Premium Add-on.

    Thread Starter Dvd709

    (@dvd709)

    EDIT: I solved it the reason it didn’t work is because I forgot to change the custom subfield at the Return Value option to ID. Stupid of me!

    Regardless of that, thanks WPRanger!

    For the future i can give You an advice.

    Instead of using subfields etc.

    U can save all the repeater field values in 1 array by:

    $repeater = get_field('repeater_name');

    Then u can var_dump($repeater);

    and use:

    foreach ($repeater as $item) :
       echo $item['my_title'].' - '.$item['my_other_field'].'<br>';
    endforeach;

    Its easier to navigate than use get_sub_field and have_rows()

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get_post is not functioning properly.’ is closed to new replies.