Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- 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;
- }
- add_action('the_content', 'do_jt_related_posts');
- function do_jt_related_posts() {
- if( is_singular('post') ) :
- echo get_the_content();
- echo pippin_related_posts();
- else :
- echo get_the_content();
- endif;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement