<?php

$orig_post	 = $post;
global $post;
$tags		 = wp_get_post_tags( $post->ID );
if ( $tags ) {
	$tags_ids = array();
	foreach ( $tags as $individual_tag ) {
		$tags_ids[] = $individual_tag->term_id;
	}
	$args = array(
		'tag__in'		 => $tags_ids,
		'post__not_in'	 => array( $post->ID ),
		'posts_per_page' => 20
	);

	$my_query = new wp_query( $args );

	$output = '';

	if ( $my_query->have_posts() ) {
		$output .= '<div class="relatedposts">
			        <div class="title">
			    		<h4>' . __( 'Related Posts', 'wiz' ) . '</h4>
			        </div>';
		$output .= '<div class="list_carousel responsive"><ul class="portfolio-slider">';

		while ( $my_query->have_posts() ) {
			$my_query->the_post();
			if ( has_post_thumbnail() ) {
				$output .= '<li>';
				$output .= '<div class="leap-overlay item-img">';
				$output .= get_the_post_thumbnail( get_the_ID(), 'leap-related-thumbnail' );
				$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
				$output .= '<div class="item-overlay">';
				$output .= '<div class="item-img-color"></div>';
				$output .= '<div class="item-details">';
				$output .= '<h4 class="item-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '">' . get_the_title() . '</a></h4>';
				$output .= '<div class="item-links">';
				$output .= '<a class="item-link" href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '">' . __( 'Link', 'wiz' ) . '</a>';
				$output .= '<a class="enlarge" data-rel="prettyPhoto[sc_related_gal]" href="' . $large_image_url[ 0 ] . '" title="' . the_title_attribute( 'echo=0' ) . '">' . __( 'Enlarge', 'wiz' ) . '</a>';
				$output .= '</div>';
				$output .= '</div>';
				$output .= '</div>';
				$output .= '</div>';
				$output .= '</li>';
			}
		}
		$output .= '</ul><div class="cfs-nav"><span class="cfs-prev">Previous</span><span class="cfs-next">Next</span></div></div></div>';
	}

	echo $output;
}
$post = $orig_post;
wp_reset_postdata();
