Skip to content

Instantly share code, notes, and snippets.

/profitmag-child
Created Nov 19, 2017

Embed
What would you like to do?
function profitmag_related_post( $post_id ) {
$categories = get_the_category( $post_id );
if( $categories ) {
$category_ids = array();
foreach( $categories as $category ) {
$category_ids[] = $category->term_id;
}
$args = array(
'category__in' => $category_ids,
'post__not_in' => array( $post_id ),
'posts_per_page' => 4,
);
$related_query = new WP_Query( $args );
if( $related_query->have_posts() ) {
echo '<ul>';
while( $related_query->have_posts()){
$related_query->the_post();
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
?>
<?php
}else {
?>
<ul><li><?php _e( 'No related post.', 'profitmag'); ?></li></ul>
<?php
}
wp_reset_postdata();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.