Skip to content

Instantly share code, notes, and snippets.

@rustam-swe
Last active October 1, 2020 07:49
Show Gist options
  • Save rustam-swe/6a2747e84474202ffd5d82b5da21b5ae to your computer and use it in GitHub Desktop.
Save rustam-swe/6a2747e84474202ffd5d82b5da21b5ae to your computer and use it in GitHub Desktop.
<?php
/**
* This template used in regular posts pages.
* $post->ID will taken from those posts
*
* @var $post
* TODO: Explain what does this code
*/
$tags = wp_get_post_tags($post->ID);
if ($tags):
$tags_slug = [];
foreach ($tags as $tag) {
$tags_slug[] = $tag->slug;
}
$args = [
'post_per_page' => 12,
'post__not_in' => [$post->ID], // Exclude current post from the list
'tag_slug__in' => $tags_slug // Posts at least with one of the given tags
];
$related_products = new WP_Query($args);
?>
<div class="col-12">
<div class="rounded_panel mt70">
<h2>Related resources</h2>
<div class="row">
<div class="col-12 ">
<ul class="relatedlinks">
<?php if ($related_products) : ?>
<?php while ($related_products->have_posts()): ?>
<?php $related_products->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
</div>
</div>
</div>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment