Gekos
10/4/2019 - 7:59 AM

Loop base di WP per scorrere gli articoli

Questo è un esempio di loop per la lettura degli articoli di WP, prima c'è l'if per determinare se è presente un articolo, poi c'è il ciclo while, dopo il ciclo while c'è il codice per la paginazione e in fine si chiude l'if

<?php if (have_posts()) :?>
  <?php while(have_posts()) : the_post(); ?>
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
          <p> <?php the_time('j M , Y') ?> - <?php the_category(', '); ?></p>
          <a href="<?php the_permalink(); ?>">
              <?php the_post_thumbnail('miotema_single', array('class' => 'img-res','alt' => get_the_title())); ?>
          </a>
          <?php the_excerpt();?>
      </article>
  <?php endwhile; ?>

  <div class="pagination">

    <?php /* Pagination */
    global $wp_query;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
    ?>
  </div>
<?php else : ?>
  <h3> <?php esc_html_e('Sorry, no posts matched your criteria.', 'miotema'); ?> </h3>
<?php endif; ?>