Advertisement
Guest User

boink

a guest
May 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. <?php /* Template Name: All Icons */ ?>
  2. <?php get_header();?>
  3. <div class="col-xs-12">
  4. <div class="container">
  5. <h1 class="text-center"><?php the_title();?></h1>
  6. <div id="" class="container">
  7.  
  8.  
  9. <?php
  10.  
  11. $string = file_get_contents("http://paulosaferreira.com/wp-content/themes/flipcard/json-files/noun-project-2016-May-24.json");
  12. $json_a = json_decode($string, true);
  13.  
  14.  
  15.  
  16.  
  17. foreach ($json_a as $icons => $icon) {
  18.  
  19.  
  20.  
  21. $icon_post = array(
  22. 'post_title' => $icon['name'],
  23. 'post_type' => 'icon',
  24. 'post_status' => 'publish',
  25. 'post_author' => 1
  26. );
  27.  
  28.  
  29.  
  30. //////////// FATURED IMAGE //////////////
  31.  
  32. $post_id = wp_insert_post($icon_post);
  33.  
  34.  
  35.  
  36.  
  37. // Add Featured Image to Post
  38. $image_url = $icon['img'] ;// Define the image URL here
  39. $upload_dir = wp_upload_dir(); // Set upload folder
  40. $image_data = file_get_contents($image_url); // Get image data
  41. $filename = basename($image_url); // Create image file name
  42.  
  43. // Check folder permission and define file location
  44. if( wp_mkdir_p( $upload_dir['path'] ) ) {
  45. $file = $upload_dir['path'] . '/' . $filename;
  46. } else {
  47. $file = $upload_dir['basedir'] . '/' . $filename;
  48. }
  49.  
  50. // Create the image file on the server
  51. file_put_contents( $file, $image_data );
  52.  
  53. // Check image file type
  54. $wp_filetype = wp_check_filetype( $filename, null );
  55.  
  56. // Set attachment data
  57. $attachment = array(
  58. 'post_mime_type' => $wp_filetype['type'],
  59. 'post_title' => sanitize_file_name( $filename ),
  60. 'post_content' => '',
  61. 'post_status' => 'inherit'
  62. );
  63.  
  64. // Create the attachment
  65. $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
  66.  
  67. // Include image.php
  68. require_once(ABSPATH . 'wp-admin/includes/image.php');
  69.  
  70. // Define attachment metadata
  71. $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
  72.  
  73. // Assign metadata to attachment
  74. wp_update_attachment_metadata( $attach_id, $attach_data );
  75.  
  76. // And finally assign featured image to post
  77. set_post_thumbnail( $post_id, $attach_id );
  78.  
  79. //////////// Custom Fields //////////////
  80.  
  81.  
  82. add_post_meta($post_id, 'icon_noun_id', $icon['id'] , true);
  83.  
  84. $info_update_date = get_the_date( $format, $post_id );
  85. $downloads_update = "downloads_update";
  86. $value = array(
  87. array(
  88. "sample_date" => $info_update_date,
  89. "download_count" => $icon['downloads']
  90. )
  91. );
  92.  
  93. update_field( $downloads_update, $value, $post_id );
  94.  
  95. }
  96.  
  97.  
  98. ?>
  99.  
  100.  
  101. <?php
  102. $icons = array(
  103. 'post_type' => 'icon',
  104. 'post_status' => 'publish',
  105. 'order' => 'DESC', // 'ASC',
  106. 'posts_per_page' => -1
  107. );
  108. $icons = new WP_Query( $icons );
  109. ?>
  110.  
  111. <?php if ( $icons->have_posts() ) : while ( $icons->have_posts() ) : $icons->the_post(); ?>
  112.  
  113. <div class="icon-item" id="icon-id- <?php the_ID(); ?> ">
  114. <img src="<?php the_post_thumbnail_url('thumbnail' );?>" alt="Icon - <?php the_title();?>" class="icon-img">
  115. <h2><?php the_title(); ?></h2>
  116.  
  117. <?php
  118. $iconID = 1;
  119. $download_update = get_field('downloads_update');
  120.  
  121. // var_dump($download_update);
  122. echo '<br>';
  123. // echo $download_update['download_count'] ;
  124. $last_update_data = $download_update[count($download_update) - 1];
  125. $pervious_update_data = $download_update[count($download_update) - 2];
  126. $last_download_count = $last_update_data['download_count'];
  127. $pervious_download_count = $pervious_update_data['download_count'];
  128. $last_update_growth = $last_download_count - $pervious_download_count ;
  129.  
  130. echo '<div class="download-count"> Total: '.$last_update_data['download_count'] . '</div>';
  131. echo '<div class="month-downloads"> This month: '. $last_update_growth . '</div>';
  132. echo '<div class="download-count"> '.$last_update_data['sample_date'] . '</div>';
  133.  
  134. $iconID++;
  135. ?>
  136.  
  137. </div> <!-- closes the first div box -->
  138.  
  139. <?php endwhile;
  140. wp_reset_postdata();
  141. else : ?>
  142. <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  143. <?php endif; ?>
  144.  
  145.  
  146.  
  147. <?php get_footer();?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement