Created
March 1, 2022 23:33
-
-
Save rhcarlosweb/235d4e8c5f592b8296bba9bd62fc2c2e to your computer and use it in GitHub Desktop.
Relacionados
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Relacionados | |
* | |
* @version 1.0 | |
* @author Rhuan <rhcarlosweb@gmail.com> | |
*/ | |
function nexo_relatedposts() { | |
/** | |
* Global posts | |
*/ | |
global $post; | |
$orig_post = $post; | |
$categories = get_the_category($post->ID); | |
/** | |
* Se houver posts na mesma categoria do artigo atual | |
*/ | |
if ($categories) { | |
/** | |
* Puxa id das categorias relacionadas ao post | |
* @var array | |
*/ | |
$category_ids = array(); | |
/** | |
* Separa categorias e mescla todas em um array | |
*/ | |
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; | |
/** | |
* Variaveis para exibir artigos relacionados | |
*/ | |
$args=array( | |
'category__in' => $category_ids, /* array de categorias relacionadas ao artigo */ | |
'post__not_in' => array($post->ID), | |
'posts_per_page' => 3, | |
'orderby' => 'rand' | |
); | |
$my_query = new wp_query( $args ); | |
/** | |
* Se houver posts relacionados na mesma categoria | |
*/ | |
if( $my_query->have_posts() ) { ?> | |
<!-- relacionados --> | |
<div class="related-posts clearfix"> | |
<!-- heading --> | |
<div class="heading-article"> | |
<span>veja também!</span> | |
</div> | |
<!-- end heading --> | |
<!-- lista relacionados --> | |
<div class="row-related"> | |
<?php while( $my_query->have_posts() ) { $my_query->the_post();?> | |
<div class="col-related"> | |
<!-- thubm --> | |
<div class="thumb-related"> | |
<a href="<?php the_permalink();?>" title="<?php the_title();?>"> | |
<?php nexo_thumbnail('', '', 300, 160, true, false); ?> | |
</a> | |
</div> | |
<!-- end thumb --> | |
<!-- name --> | |
<div class="name-related"> | |
<a href="<?php the_permalink();?>" title="<?php the_title();?>"> | |
<?php the_title(); ?> | |
</a> | |
</div> | |
<!-- end name --> | |
</div> | |
<?php } // end while ?> | |
</div> | |
<!-- end lista relacionados --> | |
</div> | |
<!-- end relacionados --> | |
<?php } // end if query | |
} // end if have posts | |
$post = $orig_post; | |
wp_reset_query(); /* reseta loop para não dar conflito com demais loops */ | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment