Created
July 26, 2023 15:21
-
-
Save Kernix13/d5d028154879f9658012db3b39a2fd11 to your computer and use it in GitHub Desktop.
WordPress Category Recent Posts
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
<!-- 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