<?php
/**
 * The template for displaying archive pages.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Optics-mws
 */

#!-- ---------------------------------------------- for infinite-scroll --- --
add_filter('next_posts_link_attributes', 'next_posts_link_attributes');
function next_posts_link_attributes() {
    return 'class="next pagination__next" style="float:right"';
}

get_header();
?>
<article id="comments-page" class="comments-page page type-page status-publish hentry entry">    
  <header class="entry-header alignwide">
    <?php
    the_archive_title( '<h1 class="page-title">', '</h1>' );
    the_archive_description( '<div class="taxonomy-description">', '</div>' );
    ?>
  </header><!-- .page-header -->
  <div class="entry-content archive-wider">
    <ul class="blocks-gallery-grid">
    <?php while ( have_posts() ) : ?>
              
      <?php
      /* get_template_part( 'template-parts/content', get_post_format() ); */
      $post = get_post(the_post());
      echo "<!-- post->ID: $post->ID -->\n";
      $image = false;
      if ( get_attached_media('image', $post->ID) ) {
          $image = array_shift(get_attached_media('image', $post->ID));
          $image = $image->ID;
          echo "<!-- image->ID: $image -->\n";
          $imgs = get_attached_media('image', $post->ID);
          foreach($imgs as $img) 
              echo "<!-- // img->ID: $img->ID -->\n";
      }
      if (! $image) {
          $image = get_post_thumbnail_id($post->ID);
          echo "<!-- get_the_post_thumbnail " . $image . " -->\n";
      }
      if ( $image ) {
          /* $thumbsrc = wp_get_attachment_image_src($image->ID, 'optics-mws-gallery-thumb')[0]; */
          $thumbsrc = wp_get_attachment_image_src($image, 'thumbnail')[0];
          $fullsrc =  wp_get_attachment_image_src($image,'full')[0];
          $style = "";
          echo "<!-- thumbsrc: $thumbsrc // fullsrc: $fullsrc -->\n";
      } else {
          $thumbsrc = get_stylesheet_uri() . '/../270.png';
          $tithumbsrc = get_stylesheet_uri() . '/../560.png';
          echo "<!-- thumbsrc: $thumbsrc // tithumbsrc: $tithumbsrc -->\n";
          $style = "style='display: inline-block !important; background: #ddd;'";
      }
      /* echo "<li class='blocks-gallery-item'><figure><a href='$fullsrc'><img src='$thumbsrc' /></a>\n"; */
      echo "<li class='archive-slide'><figure><a href='$fullsrc'><img src='$thumbsrc' class='archive-slide-slide'/></a>\n";
      echo "<div class='archive-slide-titlebar' $style><a href='", get_permalink($post->ID),
      "' title='", get_the_date('', $post->ID), "'><span>", get_the_title($post->ID), "<br />(",
      get_the_date('', $post->ID), ")</span></a></div>";   
      echo '</figure></li>';
      ?>
    <?php endwhile; ?>
    </ul>
    <?php the_posts_pagination(); ?>
  </div>
</article>

<?php 
get_footer();
?>
