0

I am trying to make use of transients in order to load some data I am fetching from another website quicker but the data is not displaying at the moment. Code Below:

    <div class="col-sm-3 news-scroller">
  <div class="slick-slider pin-news-ticker">
        <?php if (have_posts()) : $counter = 1; $alternate = 1; while (have_posts()) : the_post();?>

        <!-- Carousel items -->
          <div class="slide">
              <div class="entry entry-smallest-list">
                    <a class="entry-thumb-wrapper" href="<?php the_permalink(); ?>">
                      <div class="entry-thumb">
                        <a href="<?php the_permalink(); ?>">
                          <div class="thumb-wrapper">
                            <?php the_post_thumbnail( 'post-small-rectangle-pin' );?>
                          </div>
                        </a>
                      </div>
                    </a>
                  <div class="entry-details">
                    <div class="entry-date">
                      <small><?php echo get_the_date('M jS, Y'); ?></small>
                    </div>
                    <h4 class="entry-title">
                      <a href="<?php the_permalink(); ?>">
                        <?php echo mnr_shorten_title(get_the_title()); ?>
                      </a>
                     </h4>
                  </div>
                  <div class="clearfix"></div>
              </div> <!-- end .entry-small-list -->
            </div><!--/ .slide-->

             $trans_figures_id = 'portal_figures';
             $external_figures = get_transient( $trans_figures_id );

             if ( false === $external_figures ) {
               ob_start();

             $production_url = 'my-url';

             $production_request = wp_remote_get( $production_url );
             if(is_wp_error($production_request)) {
               return false;
             }

             $production_body = wp_remote_retrieve_body( $production_request );

             $production_data = json_decode( $production_body );


             if($counter++ % 2 == 0) :
              if($alternate++ % 2 == 1) : ?>
                <div class="slide pin-alternate-content">
                  <h3>$<?php echo esc_html( number_format_i18n( $production_data[10]->Average, 1)); ?></h3>
                  <h4>Kurdistan Blend Price per BBL</h4>
                  <small>Average of last 30 shipments</small>
                </div>
              <?php else : ?>
                <div class="slide pin-alternate-content">
                  <h3><?php echo esc_html( number_format_i18n( $production_data[9]->Average, 1 )); ?></h3>
                  <h4>Kurdistan Blend API</h4>
                  <small>Average of last 30 shipments</small>
                </div>
              <?php endif;
              endif;

                $external_figures = ob_get_clean();
                set_transient( $trans_figures_id, $external_figures, DAY_IN_SECONDS );
              }
              echo $external_figures;

           endwhile;
        endif;

        ?>

  </div>
</div><!-- end .col-sm-3 -->

Without the transient the content displays perfectly but it takes ages to load. With the transient the content simply does not display at all. How can i tweak this code in order to make it work with the transient?

0

You write "if ( false === $external_figures )" but forgot write "else" with echo $external_figures Example:

if ( false !== $external_figures ) {
echo $external_figures;
}else{
 ob_start();
 $production_url = 'my-url';
 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.