Support » Developing with WordPress » transient api json shortcode

  • Hi,

    I want to transient my API can anyone please help me with my problem.

    Please see my sample code below.

    
    //league-leaders
    function test_api_leaders_shortcode( $atts , $content = null){
      // Attributes
      $atts = shortcode_atts(
        array(
                'comid' => '16395',
                'statistic' => 'sPointsAverage',
        ),
        $atts,
        'test-api-leaders'
      );
    
    $competition = $atts['comid'];
    
    $statisticsz = $atts['statistic'];
    
    $request = wp_remote_get( 'https://api.wh.geniussports.com/v1/basketball/competitions/'. $competition .'/leaders/summary?ak=eebd8ae256142ac3fd24bd2003d28782&statisticCodes='. $statisticsz .'&limit=5' );
    
    if( is_wp_error( $request )) {
    
    return false; // Bail early
    
    }
    //for-leaders
    $body = wp_remote_retrieve_body( $request );
    $data = json_decode( $body );
    
    $list = '<table class="rwd-table">'.'<tr><th>Player Name</th><th>Team</th><th>Data</th></tr>'; 
    
    if( ! empty( $data ) ) {
    $wewLeaders = $data->response;
    
    foreach($wewLeaders->data as $product ) {
    
      foreach($product->leaders as $product2 ) {
      
      //firstname-cut
      $fname = explode(' ',trim($product2->firstName));
      
      $fnamesz = strtolower($fname[0]);
      
      //lastname-capitalize;
      $lname = strtolower($product2->familyName);
      
    
    $list .='<tr><td data-th="Movie Title">'. $fnamesz .' '. $lname .'</td><td data-th="Genre">' . $product2->teamName . '</td> <td data-th="Year">' . $product2->value . '</td></tr>';
    
    }
    
    }//endforeach
    
    }// end if
    
    return $list . '</table>';
    
    }
    
    add_shortcode('test-api-leaders', 'test_api_leaders_shortcode');
    
Viewing 1 replies (of 1 total)
  • Before you generate the output, check for the transient. If it exists, use it, else generate the output and set the transient.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.