Skip to content

Instantly share code, notes, and snippets.

@Kernix13
Created July 26, 2023 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kernix13/d5d028154879f9658012db3b39a2fd11 to your computer and use it in GitHub Desktop.
Save Kernix13/d5d028154879f9658012db3b39a2fd11 to your computer and use it in GitHub Desktop.
WordPress Category Recent Posts
<!-- Add before endwhile in single.php or as a plugin -->
<aside class="bgcolor4">
<div class="container">
<h3 class="custom-title"><?php esc_html_e('Similar articles you may like... ', 'tower') ?></h3>
<div class="row">
<?php
$cats = wp_get_post_categories( get_the_ID(), ['fields'=>'ids',]);
$relatedPosts = get_posts(
[ 'numberposts'=> 3,
'category'=> implode(',', $cats),
'post__not_in'=> [get_the_ID(),],
]);
global $post;
if ( $relatedPosts ) {
foreach ($relatedPosts as $post) {
setup_postdata($post); ?>
<div class="single-recent-row">
<a class="single-recent-posts-link" href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail() ?></a>
<a class="single-recent-posts-link" href="<?php the_permalink(); ?>" rel="bookmark"><h4 class="single-recent-row-title"><?php the_title(); ?></h4></a>
<?php get_template_part( 'template-parts/content', 'author' ); ?>
</div>
<?php
}
wp_reset_postdata();
} else {
echo "No other articles in this category...for now";
}
?>
</div>
</div>
</aside>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment