brunopulis
12/24/2018 - 1:09 PM

related-post.php

<section class="related-post">
  <?php
    $categories = get_the_category( $post->ID );

    if ( $categories ) {
      $category_ids = array();

      foreach($categories as $individual_category)
        $category_ids[] = $individual_category->term_id;
        $args = array (
          'category__in' => $category_ids,
          'post__not_in' => array($post->ID),
          'showposts' => 3,
          'caller_get_posts' => 1
        );

        $cat_query = new WP_Query( $args );

        if( $$cat_query->have_posts() ) {
          $count = 1;
  ?>
    <h2 class="related-post__title">Artigos relacionados</h2>

    <?php while ($$cat_query->have_posts()) { $$cat_query->the_post(); ?>
      <article class="col-xs-12 col-sm-4 <?php if($count == 4) echo "last" ?>">
        <div class="related-post__card">
          <?php if( has_post_thumbnail() ):  ?>
            <figure>
              <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink() ?>">
                <?php the_post_thumbnail('thumb-related-post');?>
              </a>
            </figure>
          <?php else : ?>
            <figure>
              <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink() ?>">
                <img src="http://placehold.it/260x210" alt="">
              </a>
            </figure>
          <?php endif; ?>

          <div class="related-post__description">
            <?php $category = get_the_category(); ?>
            <header class="related-post__header">
              <span class="related-post__category"><?php echo $category[0]->cat_name; ?></span>
              <h3 class="related-post__headline">
                <a href="<?php the_permalink() ?>" rel="bookmark">
                  <?php the_title(); ?>
                </a>
              </h3>
            </header>

            <div class="related-post__body">
              <p><?php echo excerpt(15); ?></p>
            </div>

            <a href="<?php the_permalink(); ?>" class="related-post__link">Leia mais <span class="sr-only">sobre <?php the_title(); ?></span></a>
          </div>
        </div>
      </article>

    <?php $count ++; ?>
    <?php }
                wp_reset_query();
            }
        }
    ?>
    </div>
</section>