• hi I would like to obtain data from JSON (e.g: the title & thumbnail) instead of displaying title from default post title.

    here’s my code (template-parts) for home page:

    <?php
    $id = get_post_meta( get_the_ID(), 'itunes_id', true );
    $country = get_post_meta( get_the_ID(), 'select_country', true );
    $json = file_get_contents("https://itunes.apple.com/$country/lookup?id=$id&entity=song");
    $data = json_decode($json);
    
    foreach ($data->results as $row){
       $aw = $row->artworkUrl100;
    ?>
    
       <a class="hidden-last-child" href="<?php the_permalink() ?>">
          <div class="store-product">
              <figure>
                  <img src="<?php echo str_replace("100x100bb","313x0w","$aw"); ?>" alt="<?php echo $row->artistName . ' - ' . $row->collectionName; ?>">
              </figure>
    
                <div class="music-title">
                <h2 class="album-title">
                      <?php
                      if ( in_category( 4 ) || in_category( 6 ) ) {
                            echo $row->trackName;
                      }  else {
                            echo $row->collectionName;
                      }
                      ?>
                   </h2>
                  <h3 class="artis-title"><?php echo $row->artistName; ?></h3>
                </div>
          </div>
       </a>
    
    <?php
    break;
    }
    ?>

    How can I resolve this?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @odiodi,

    I don’t see how your question is related to my plugin. Perhaps you posted on the wrong forum by mistake? Try asking your question here instead.

    Thread Starter Ardhia Mangku

    (@odiodi)

    @hcabrera
    it’s related to your plugin.
    I only get output:
    <ul class=”wpp-list”>

      it should be the same as the code above.

      I use this code to display custom popular post

      <?php
      $query =
         array(
          'cat' => '2,4,6',
          'post_type' => 'post',
          'limit' => 20,
          'range' => 'weekly',
         );
      
      $query = wpp_get_mostpopular($query);
      if ( $query->have_posts() ) :
         if ( is_home() ) :
         endif;
         while ( $query->have_posts() ) :
            $query->the_post();
            get_template_part( 'template-parts/content-album-page', get_post_type() );
         endwhile;
      else :
         get_template_part( 'template-parts/content', 'none' );
      endif;
      ?>
    Thread Starter Ardhia Mangku

    (@odiodi)

    so how do I display custom output like <div class=”store-product”>… instead of <ul class=”wpp-list”>?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey Ardhia,

    This is what you said:

    I would like to obtain data from JSON (e.g: the title & thumbnail) instead of displaying title from default post title.

    In your code, you have:

    $json = file_get_contents("https://itunes.apple.com/$country/lookup?id=$id&entity=song");

    It seems you’re trying to fetch song data (title and thumbnail) from the iTunes API. Your question is more general programming related and so it belongs to the Miscellaneous forum. It’s not like I don’t want to help you out but this topic has nothing to do with how the plugin works (and I’m not even familiar with the iTunes API so you’re asking the wrong person anyways).

    Also, please use the code button to wrap code when posting it on the forums.

    Plugin Author Hector Cabrera

    (@hcabrera)

    so how do I display custom output like <div class="store-product">… instead of <ul class="wpp-list">?

    Now this is related to the plugin, this I can answer. Use the wpp_start and wpp_end parameters to change the wrapper element, like so:

    $query = array(
        'cat' => '2,4,6',
        'post_type' => 'post',
        'limit' => 20,
        'range' => 'weekly',
        'wpp_start' => '<div class="store-product">',
        'wpp_end' => '</div>',
    );

    For a complete list of parameters, please check Settings > WordPress Popular Posts > Parameters.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Obtain data from JSON’ is closed to new replies.