Support

Account

Home Forums Add-ons Repeater Field Issue with While Loop and Fragment Cache

Solving

Issue with While Loop and Fragment Cache

  • Hey all,

    So I have the following function in my functions.php file for creating fragment cache objects:

    // Fragments caching function
    function fragment_cache($key, $ttl, $function) {
        if ( is_user_logged_in() ) {
          call_user_func($function);
          return;
        }
        $key = apply_filters('fragment_cache_prefix','fragment_cache_').$key;
        $output = get_transient($key);
        if ( empty($output) ) {
          ob_start();
          call_user_func($function);
          $output = ob_get_clean();
          set_transient($key, $output, $ttl);
        }
        echo $output;
    }

    I then create a shortcode that uses a few ACF fields:

    <?php fragment_cache('bed_sizes_tabs', 604800, function() { ?>
                <?php while( have_rows('tab_content', $page_id) ): the_row();             
                    // Nav Vars
                    $tab_image = get_sub_field('tab_image', $page_id);
                    $tab_title = get_sub_field('tab_title', $page_id); 
                ?>
                    <!-- Tab Bed Icons -->
                    
                        <a aria-controls="tab-item-<?php echo get_row_index(); ?>" aria-selected="true" class="tab-nav tab-apha <?php if (get_row_index() == 1) echo "active"; ?>" data-toggle="tab" href="#tab-item-<?php echo get_row_index(); ?>" role="tab">
                            <img src="<?php echo esc_url($tab_image['url']) ?>">
                            <?php echo $tab_image['url'] ?>
                            <?php echo $tab_image ?>
                            <?php echo get_sub_field('tab_image')['url']; ?>
                            <div class="mobile-bed-size"><?=$tab_title?></div>
                            <span></span>
                        </a> 
                    
                    <!-- End Tab Bed Icons -->
                <?php endwhile; ?>
    <?php }); ?>

    Notice these lines, I added them for debugging purposes:

    
    <?php echo $tab_image['url'] ?>
    <?php echo $tab_image ?>
    <?php echo get_sub_field('tab_image')['url']; ?>
    

    The first line returns the value “6”. The 2nd line returns the ID of the image. The 3rd line returns the URL, as I want it.

    Why does setting a variable and then returning the [‘url’] from the array not work? But getting the field without a variable works?

    I hope my question makes sense?

    Kind regards
    Chris

  • Looking at your code, my first question is, what is the value of $page_id. It’s not defined. On this line have_rows('tab_content', $page_id) ) since it is not defined then ACF is assuming the current post ID, whatever that might be.

    Second part get_sub_field('tab_image', $page_id); for sub fields you do not need to specify the post ID. This second parameter is whether or not to format the value. Since $page_id is undefined at this point, or null, that equals false and this is causing ACF to return the unformatted value of the sub field, in this case the attachment ID.

  • Hey there,

    $page_id is set just above the snippet. However, it was causing the issue. When I removed $page_id, I got it working.

    Thanks
    Chris

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

The topic ‘Issue with While Loop and Fragment Cache’ is closed to new replies.