Skip to content

Commit 7e6c544

Browse files
authored
Related posts: fix WPCS violations (#25)
Fixes WPCS violations, minor code smells and a typo in the related posts snippet
1 parent 1555e01 commit 7e6c544

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/show_related_posts_on_single.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
<?php
22

3-
// Show related posts @ single post
3+
// Show related posts @ single post.
44

5+
/**
6+
* Show random posts from the same categories as the current post.
7+
*
8+
* @return void
9+
*/
510
function related_posts() {
611

7-
// current post id
12+
// current post id.
813
$current_post_id = get_the_ID();
914

1015
$related = new WP_Query(
1116
array(
1217
'category__in' => wp_get_post_categories( $current_post_id ),
1318
'posts_per_page' => 4,
1419
'orderby' => 'rand',
15-
'post__not_in' => array( $current_post_id ) // exlcude currnet post
20+
'post__not_in' => array( $current_post_id ), // exclude current post.
1621
)
1722
);
1823

1924
if ( $related->have_posts() ) {
2025

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

23-
while ( $related->have_posts() ) { $related->the_post();
28+
while ( $related->have_posts() ) {
29+
$related->the_post();
2430

25-
// related post data
26-
$post_id = get_the_ID();
27-
$post_title = get_the_title( $post_id );
28-
$post_permalink = get_the_permalink( $post_id );
29-
$post_featured_image = get_the_post_thumbnail( $post_id, 'medium' );
31+
// entry div structure.
32+
echo '<div class="related-post-item">';
33+
echo '<div class="post-featured-image">';
34+
the_post_thumbnail( 'medium' );
35+
echo '</div>';
36+
echo '<h3 class="post-title">' . esc_html( get_the_title() ) . '</h3>';
37+
echo '<a href="' . esc_url( get_the_permalink() ) . '" class="button">Read More</a>';
38+
echo '</div>';
3039

31-
// entry div structure
32-
echo '<div class="related-post-item">';
33-
echo '<div class="post-featured-image">' . $post_featured_image . '</div>';
34-
echo '<h3 class="post-title">' . $post_title . '</h3>';
35-
echo '<a href="' . $post_permalink . '" class="button">Read More</a>';
36-
echo '</div>';
37-
38-
}
40+
}
3941

4042
echo '</div>';
4143

0 commit comments

Comments
 (0)