<?php
	get_header();
?>			
	<div class="container">
		<div class="row">
		<?php $termz = $wp_query->queried_object;?>
			<div class="content col-xs-12 col-md-8 col-lg-8">
		<?php $categories = get_terms('categories_blog');
			foreach ($categories as $categori) {   ?>
		<?php if ($categori->parent==$termz->term_id) {?>
			 <div class="category_menu">
			 	<a href="/<?php echo $categori->taxonomy ?>/<?php echo $categori->slug ?>">
					 <div class="img_menu">
			 			<img src="<?php echo z_taxonomy_image_url($categori->term_id); ?>" />
			 		</div>
			 		<div class="text_menu">
			 			<?php echo $categori->name ?>
					</div>
				</a>
			</div>
		<?php }else{}
			 ?>
		<?php  }?>
				<div class="loop_content">
						<div class="loop_blog row">
						<div class="post_title"><?=$termz->name?></div>
							<?php 
							$args = array(
								'posts_per_page' => 10,
								'orderby' => 'ASC',
								'paged' => $paged,
								'post_type' => 'blog',
								'tax_query' => array(array( 'taxonomy' => 'categories_blog', 'field' => 'slug', 'terms' => $termz->slug))
							);

							$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-6 col-lg-6">	
						<div class="post_top archi_post">	
							<div class="thumbnail_post archi_post">
								<a href="<?php the_permalink(); ?>">
									<?php the_post_thumbnail(); ?>
								</a>
							</div>	
								<div class="expert_title archi_post">	
									<a href="<?php the_permalink(); ?>">
										<h2><?php the_title(); ?></h2>
									</a>
									<div class="expert">
										<?php echo wp_trim_words( get_the_content(), 11); ?>
									</div>
								</div>
						</div>			
					</div>
					
					<?php 
								}
							} else {
								// Постов не найдено
								echo '<p class="noposts">Рубрика в стадии наполнения!</p>';
							}
							wp_reset_postdata();?>
							
            </div>
                </div>
                	<div id="pagination"><?php echo paginate_links(); ?></div>

				<!-- SEO TEXT -->
					<div class="seo">	
					<?php 
						$post = get_post($post_id = 58); 
						echo '<p>'.$post->post_content.'</p>'; 
					?>
					</div>
				<!-- SEO TEXT -->
					
		    </div>
				    	<!-- сайдбар -->
					<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' => 'comment_count',
								'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 $termz = get_the_terms( $post->ID, 'categories_blog' );?>
			<?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>					
	</div>									
<?php
	get_footer();
?>