Skip to content

Instantly share code, notes, and snippets.

@hmboyd
Last active January 29, 2022 21:28
Show Gist options
  • Save hmboyd/57789df146a3b70ab3cb49eb4d8f35f8 to your computer and use it in GitHub Desktop.
Save hmboyd/57789df146a3b70ab3cb49eb4d8f35f8 to your computer and use it in GitHub Desktop.
1. Open container2. Get posts that this post's category is3. Posts per page4. Display other posts that aren't this post5. For every post in this query, display certain meta info (inside <li>)6. Reset post data
<!-- Custom Related Posts -->
<div class="single_related-posts">
<?php foreach((get_the_category()) as $category) : ?>
<h3>Browse more <?php echo $category->name; endforeach;?></h3>
<ul>
<?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'posts_per_page' => 6,
'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</li>
<?php }
wp_reset_postdata(); ?>
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment