Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/show_related_posts_on_single.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
<?php

// Show related posts @ single post
// Show related posts @ single post.

/**
* Show random posts from the same categories as the current post.
*
* @return void
*/
function related_posts() {

// current post id
// current post id.
$current_post_id = get_the_ID();

$related = new WP_Query(
array(
'category__in' => wp_get_post_categories( $current_post_id ),
'posts_per_page' => 4,
'orderby' => 'rand',
'post__not_in' => array( $current_post_id ) // exlcude currnet post
'post__not_in' => array( $current_post_id ), // exclude current post.
)
);

if ( $related->have_posts() ) {

echo '<div class="all-related-posts">';

while ( $related->have_posts() ) { $related->the_post();
while ( $related->have_posts() ) {
$related->the_post();

// related post data
$post_id = get_the_ID();
$post_title = get_the_title( $post_id );
$post_permalink = get_the_permalink( $post_id );
$post_featured_image = get_the_post_thumbnail( $post_id, 'medium' );
// entry div structure.
echo '<div class="related-post-item">';
echo '<div class="post-featured-image">';
the_post_thumbnail( 'medium' );
echo '</div>';
echo '<h3 class="post-title">' . esc_html( get_the_title() ) . '</h3>';
echo '<a href="' . esc_url( get_the_permalink() ) . '" class="button">Read More</a>';
echo '</div>';

// entry div structure
echo '<div class="related-post-item">';
echo '<div class="post-featured-image">' . $post_featured_image . '</div>';
echo '<h3 class="post-title">' . $post_title . '</h3>';
echo '<a href="' . $post_permalink . '" class="button">Read More</a>';
echo '</div>';

}
}

echo '</div>';

Expand Down