Instantly share code, notes, and snippets.
Forked from raselahmed7/related-posts.php
Last active
December 26, 2019 21:34
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Asikur22/add430852edf6d287aca to your computer and use it in GitHub Desktop.
[WordPress Related Post]
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
<?php | |
/* | |
* Plugin Name: Related Post | |
*/ | |
function pippin_related_posts( $taxonomy = '' ) { | |
global $post; | |
if ( $taxonomy == '' ) { | |
$taxonomy = 'post_tag'; | |
} | |
$tags = wp_get_post_terms( $post->ID, $taxonomy ); | |
if ( $tags ) { | |
$first_tag = $tags[0]->term_id; | |
$second_tag = $tags[1]->term_id; | |
$third_tag = $tags[2]->term_id; | |
$args = array( | |
'post_type' => get_post_type( $post->ID ), | |
'posts_per_page' => 4, | |
'tax_query' => array( | |
'relation' => 'OR', | |
array( | |
'taxonomy' => $taxonomy, | |
'terms' => $second_tag, | |
'field' => 'id', | |
'operator' => 'IN', | |
), | |
array( | |
'taxonomy' => $taxonomy, | |
'terms' => $first_tag, | |
'field' => 'id', | |
'operator' => 'IN', | |
), | |
array( | |
'taxonomy' => $taxonomy, | |
'terms' => $third_tag, | |
'field' => 'id', | |
'operator' => 'IN', | |
) | |
) | |
); | |
$related = get_posts( $args ); | |
$i = 0; | |
if ( $related ) { | |
global $post; | |
$temp_post = $post; | |
foreach ( $related as $post ) : setup_postdata( $post ); | |
$content .= '<ul class="related-posts-box">'; | |
$content .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>'; | |
$content .= '</ul>'; | |
endforeach; | |
$post = $temp_post; | |
} | |
} | |
return $content; | |
} | |
function do_jt_related_posts() { | |
if ( is_singular( 'post' ) ) : | |
echo get_the_content(); | |
echo pippin_related_posts(); | |
else : | |
echo get_the_content(); | |
endif; | |
} | |
add_action( 'the_content', 'do_jt_related_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
<?php | |
if ( is_singular( 'post' ) ) { | |
$aa_show_all_posts = true; | |
$aa_show_from_cat = true; | |
$tag_ids = array(); | |
$category_ids = array(); | |
$tags = wp_get_post_tags( get_the_ID() ); | |
if ( ! empty( $tags ) ) { | |
foreach ( $tags as $tag ) { | |
$tag_ids[] = $tag->term_id; | |
if ( $aa_show_from_cat == false ) { | |
continue; | |
} | |
$aa_show_from_cat = $tag->count > 1 ? false : true; | |
} | |
} | |
if ( $aa_show_from_cat ) { | |
$categories = get_the_category( get_the_ID() ); | |
if ( ! empty( $categories ) ) { | |
foreach ( $categories as $category ) { | |
$category_ids[] = $category->term_id; | |
if ( $aa_show_all_posts == false ) { | |
continue; | |
} | |
$aa_show_all_posts = $category->count > 1 ? false : true; | |
} | |
} | |
} | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page' => 4, | |
'post__not_in' => array( get_the_ID() ) | |
); | |
$args['tag__in'] = $tag_ids; | |
if ( ! $aa_show_all_posts ) { | |
$args['category__in'] = $category_ids; | |
} | |
$related_posts = new WP_Query( $args ); | |
?> | |
<div class="app-box related-posts"> | |
<div class="row"> | |
<div class="col-12"> | |
<h3 class="fa-title"><?php _e( 'Related Posts', AA_THEME_SLUG ); ?></h3> | |
</div> | |
</div> | |
<div class="row app-box-content"> | |
<?php | |
if ( $related_posts->have_posts() ) : | |
$related_posts_num = 1; | |
?> | |
<?php while ( $related_posts->have_posts() ) : ?> | |
<?php $related_posts->the_post(); ?> | |
<div class="col-12 text-center text-md-left col-md-<?php echo $related_posts->post_count > 3 ? '3' : '4'; | |
echo $related_posts_num != 1 ? ' mt-5 mt-md-0' : ''; ?>"> | |
<?php if ( has_post_thumbnail() ) : ?> | |
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> | |
<?php the_post_thumbnail( 'medium', array( 'class' => 'post-img' ) ); ?> | |
</a> | |
<?php endif; ?> | |
<h3 class="post-title"> | |
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> | |
<?php echo esc_html( wp_trim_words( get_the_title(), 5, '...' ) ); ?> | |
</a> | |
</h3> | |
<p class="post-excerpt"> | |
<?php echo esc_html( wp_trim_words( get_the_excerpt(), 20, '...' ) ); ?> | |
</p> | |
</div> | |
<?php $related_posts_num ++; ?> | |
<?php endwhile; ?> | |
<?php else : ?> | |
<div class="col-12"> | |
No Related Post Found! | |
</div> | |
<?php endif; ?> | |
</div> | |
</div> | |
<?php wp_reset_postdata(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment