<div class="print"><?php
	get_header();
?>
</div>
		<div class="container">
			<div class="row">
				<div class="single_left">
					<div class="content">
						<div class="content_bg">
							<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
							<h2><?php the_title(); ?></h2>
							<?php the_content(); ?>
							<p>Дата публикации: <?php the_date('j F Y'); ?></p>	
<div class="print"><div class="ya-share2" data-services="vkontakte,facebook,moimir,twitter" data-counter=""></div>
<div class="print_icon"><a id="print" title="Версия для печати" href="javaScript:window.print();"></a></div>
	
	<div class="row">
		<h3>Вам это может быть интересно!</h3>
			<?php setPostViews($post->ID);?>
			<?php $metki = get_the_terms( $post->ID, 'tags_cp' );?>
			<?php $termz = get_the_terms( $post->ID, 'categories_blog' );?> 
		<?php if(!empty($metki)){foreach($metki as $metka){$metka_a[] = $metka->name;}}?>
		<?php if(empty($metka_a)){}else{?>
		<?php $query = new WP_Query( array( 'post_type' => 'blog', 'order' => 'DESC', 'posts_per_page' => 100) );?>
	   	<?php while ( $query->have_posts() ) {$query->the_post();?>
	    <?php $metki_in = get_the_terms( $post->ID, 'tags_cp' );?>
		<?php $metka_z = array();?>
	    <?php if($post->ID != $pid && !empty($metki_in)){?>
	    <?php foreach($metki_in as $metkaz){$metka_z[] = $metkaz->name;}?>
	    <?php $result = array_intersect($metka_a, $metka_z);?>

	    
	    

	    <?php if(!empty($result[0])){?>
	    <?php $most_view_postz[$post->ID]['count'] = getPostViews($post->ID);?>
	    <?php $tags_posts_sort[$post->ID]['count'] = getPostViews($post->ID);?>
	    <?php $tags_posts_sort[$post->ID]['link'] = get_the_permalink($post->ID);?>
	    <?php $tags_posts_sort[$post->ID]['title'] = get_the_title($post->ID);?>
	    <?php $tags_posts_sort[$post->ID]['img'] = get_the_post_thumbnail($post->ID, 'post-thumbnail');?>
	    <?php }else{}?>
	    <?php }} wp_reset_postdata(); ?>

	    <?php arsort($most_view_postz);
	     foreach ($most_view_postz as $key => $val) {
	        $res_sort_arr[] = $key;
	        $res_sort_arr1[] = $key.'-'.$val;
	    }?>

        <?php $pop = (count($res_sort_arr)>5)? 5 : count($res_sort_arr);?>

		<?php for($z = 1; $z <= $pop; $z++){?>
		<?php $iddz = $res_sort_arr[$z];?>
		     <div class="col-xs-6 col-md-4 col-lg-2">
		        <div class="pop_post">
			        <a href="<?=$tags_posts_sort[$iddz]['link'];?>">
			         	<div class="pop_img">
			         		<?=$tags_posts_sort[$iddz]['img'];?>
			         	</div>
			         	<div class="pop_title">
			         		<?=$tags_posts_sort[$iddz]['title'];?>
			         	</div>
			     	 </a>
		     	</div>
		     </div>
		<?php }}?>   
	</div>
							<?php comments_template(); ?>
							<?php endwhile; else: ?>
							<p><?php _e('Ни одной записи не найдено!'); ?></p>
							<?php endif; ?>	
</div>							
					</div>		
				</div>
			</div>
		<div class="print">				
			<div class="single_right">
				<div class="obyavleniya">
					<div class="loop_content">
						<div class="post_obyavleniya">
						<div class="title_obyavleniya">Объявления</div>
							<?php 
							$args = array(
								'posts_per_page' => 10,
								'orderby' => 'ASC',
								'post_type' => 'obyavleniya',
							);

							$query = new WP_Query( $args );

							if ( $query->have_posts() ) {
								while ( $query->have_posts() ) {
									$query->the_post();?>

					<div class="content_post col-xs-12 col-md-12 col-lg-12">	
						<div class="post_top">	
							<div class="thumbnail_post">
								<a href="<?php the_permalink(); ?>">
									<?php the_post_thumbnail(); ?>
								</a>
							</div>	
								<div class="expert_titleobya">	
									<a href="<?php the_permalink(); ?>">
										<h2><?php title_chars(60, '...'); ?></h2>
									</a>
								</div>
						</div>			
					</div>
					<?php 
								}
							} else {
								// Постов не найдено
								echo '<p class="noposts">Рубрика в стадии наполнения!</p>';
							}
							wp_reset_postdata();?>
						<div class="morelink"><a href="<?php echo home_url('/obyavleniya'); ?>">Посмотреть все</a></div>
					</div>
		<div class="post_pop">
			<div class="title_pop">Популярные посты</div>				
			<?php $pid = $post->ID;?>
			<?php $tid = (!empty($termz[0]->parent))? $termz[0]->parent : $termz[0]->term_id;?>
			<?php $ell_sort_array = array();?>
			<?php $query = new WP_Query( array( 'post_type' => 'blog', 'order' => 'DESC', 'posts_per_page' => 200, 'tax_query' => array(array( 'taxonomy' => 'categories_blog', 'field' => 'term_id', 'terms' => $tid))) );?>

			<?php while ( $query->have_posts() ) {
								$query->the_post(); ?>
				<?php if($post->ID != $pid){?>
				<?php $count = getPostViews($post->ID);?>
				<?php $ell_sort_array[$post->ID] = $count;?>

				<?php }}?>
				<?php arsort($ell_sort_array);
				    $i = 0;
					foreach ($ell_sort_array as $key => $val) {
						if($i<5){
							 $res_sort[] = $key;
							 $res_sort1[] = $key.'-'.$val;
						}
					   $i++;
				}?>

				<?php foreach ($query->posts as $posts_for) {
					$p_f[$posts_for->ID]['title'] =  $posts_for->post_title;
					$p_f[$posts_for->ID]['link'] =  get_the_permalink($posts_for->ID);
					}?>

				<?php $pop2 = (count($res_sort)>5)? 5 : count($res_sort);?>

				<?php for($j = 0; $j < $pop2; $j++){?>
				<?php $idd = $res_sort[$j];?>
				<h3><a href="<?=$p_f[$idd]['link'];?>"><?=$p_f[$idd]['title'];?></h3></a>	
					<?php }?>

				
				 <?php wp_reset_postdata();?>
				<div class="morelink"><a href="<?php echo home_url('/blog'); ?>">Посмотреть все</a></div>			
			</div>
		</div>		
					<?php if ( is_active_sidebar( 'true_side' ) ) : ?>
							<div id="true-side" class="sidebar">
								<?php dynamic_sidebar( 'true_side' ); ?>
							</div>
					<?php endif; ?>
					<div class="row">	
					<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
						<!-- SidebarAd -->
						<ins class="adsbygoogle"
						 	style="display:block"
						 	data-ad-client="ca-pub-7105617870736530"
						 	data-ad-slot="7383541917"
						 	data-ad-format="auto"></ins>
					<script>
						(adsbygoogle = window.adsbygoogle || []).push({});
					</script>
					</div>
			</div>			
		</div>
	</div>		
</div> <!-- row обертка -->
</div>
<?php
	get_footer();
?>


