<?php

/*
 * Template Name: Publications
 *
 * @package far_loop
 *
 */
	
get_header();

?>

<div class="u_pt-3 u_pt-md-6">
    <div class="l_container u_mv-0">
      <div class="u_mb-3 u_mb-md-0">
        <!-- page-title module-->
        <div class="page-title u_mb-3 u_mb-md-4">
          <!-- Page title-->
          <h1 class="page-title__apex-title u_mb-1p5"><?php the_title();?></h1>
        </div>
      </div>
      
        <?php
        $args = array(
          'post_type'      => 'publication',
          'posts_per_page' => -1,
          'no_found_rows'  => true,
          'post_status'    => 'publish',
          'ignore_sticky_posts' => true,
        );
        $publications = new WP_Query($args);

        if ( $publications->have_posts() ) :
          echo '<div class="archive-grid">';
          while ( $publications->have_posts() ) : $publications->the_post();

            // ACF cover (array) → intenta tamaños optimizados
            $cover = get_field('cover'); // array | null
            $src   = '';
            if ( is_array($cover) ) {
              if ( !empty($cover['sizes']['large']) ) {
                $src = $cover['sizes']['large'];
              } elseif ( !empty($cover['sizes']['medium_large']) ) {
                $src = $cover['sizes']['medium_large'];
              } elseif ( !empty($cover['url']) ) {
                $src = $cover['url'];
              }
            }
            if ( ! $src ) {
              $src = get_template_directory_uri() . '/img/horizontal-placeholder.svg';
            }

            $title   = get_the_title();
            $authors = get_field('authors'); // texto (máx. 60 chars comentado)
            $year    = get_field('year');    // opcional si existe en tu ACF

            echo '<div class="archive-item">';
            echo '  <a href="' . esc_url( get_permalink() ) . '" class="archive-new-thumb">';
            echo '    <div class="archive-thumb__image-container publication">';
            echo '      <img src="' . esc_url($src) . '" alt="' . esc_attr($title) . '">';
            echo '    </div>';
            echo '    <div class="archive-thumb__text">';
            echo '      <h3 class="archive-thumb__title">' . esc_html($title) . '</h3>';

            //if ( !empty($authors) ) {
              //echo '      <div class="archive-thumb__label">' . esc_html($authors) . '</div>';
            //}
            if ( !empty($year) ) {
              echo '      <div class="archive-thumb__year">' . esc_html($year) . '</div>';
            }

            echo '    </div>';
            echo '  </a>';
            echo '</div>';

          endwhile;
          echo '</div>'; // .archive-grid
        endif;

        wp_reset_postdata();
        ?>
      

    </div>
  </div>
  
<?php

get_footer();
