<?php
/**
 * Template Name: template full width con sidebar
 * Template Post Type: post, page
 *
 * @package ilex
 * @since 1.0
 */
get_header();
?>

<div class="container full">
  <main id="site-content" role="main" >
	  	<?php
		while ( have_posts() ) : the_post();?>
		<article <?php post_class(); ?>>
        <h1 class="mb-3 mt-2 aligncenter"><?php the_title(); ?></h1>


        <?php
        if(has_post_thumbnail()) {
          the_post_thumbnail('large', array( 'class' => 'img-res', 'alt' => get_the_title() ));
        }
        ?>
        <div class="entry-content">
          <?php
          the_content(
            sprintf(
              wp_kses(
                /* translators: %s: Name of current post. Only visible to screen readers */
                __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'ilex' ),
                array(
                  'span' => array(
                    'class' => array(),
                  ),
                )
              ),
              get_the_title()
              )
            );
            ?>
            </div>
            <div class="entry-content-bottom">
            <?php wp_link_pages('pagelink=Page %'); ?>

            <?php the_tags(); ?>
            <div class="spacer"></div>
            <hr>

            <?php
            the_post_navigation(
              array(
                'prev_text' => '<span class="nav-title">        
                <svg
                viewBox="0 0 1792 1792"
                xmlns="http://www.w3.org/2000/svg" >
                <path d="m 880,1251 q -13,0 -23,-10 L 391,775 q -10,-10 -10,-23 0,-13 10,-23 l 50,-50 q 10,-10 23,-10 13,0 23,10 l 393,393 393,-393 q 10,-10 23,-10 13,0 23,10 l 50,50 q 10,10 10,23 0,13 -10,23 l -466,466 q -10,10 -23,10 z"/>
                </svg> %title</span>',
                'next_text' => '<span class="nav-title">%title         
                <svg
                viewBox="0 0 1792 1792"
                xmlns="http://www.w3.org/2000/svg" >
                <path d="m 880,1251 q -13,0 -23,-10 L 391,775 q -10,-10 -10,-23 0,-13 10,-23 l 50,-50 q 10,-10 23,-10 13,0 23,10 l 393,393 393,-393 q 10,-10 23,-10 13,0 23,10 l 50,50 q 10,10 10,23 0,13 -10,23 l -466,466 q -10,10 -23,10 z"/>
                </svg></span>',
              )
            );
            ?>

            <hr>
            <div class="spacer"></div>
			<div class="info-article">
<p class="small"> Data pubblicazione: <?php the_time(get_option('date_format')); ?> 
=> Ultimo aggiornamento <time datetime="<?php the_modified_time('Y-m-d'); ?>"><?php the_modified_time(get_option('date_format')); ?></time></p>

<p class="small">Autore: <?php the_author_posts_link(); ?> </p> 
<p class="small">Categorie: <?php the_category(', ');?></p>
<p class="small">Tag: <?php the_tags(' ') ?></p>
<div class="spacer"></div>
</div>
 <hr>
             <div class="spacer"></div>
            <div class="comments">

              <?php comments_template(); ?>

            </div>
        </div>
		</article> 
		<?php endwhile; // End of the loop.
		?>
  </main>
  </div>
 <?php if(is_active_sidebar('sidebar-1') ) { ?>
  <aside id="site-complementary" role="complementary" class="col-19">
<div class="correlated wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<?php
$categories = get_the_category($post->ID);

if ($categories) {
    $category_ids = array();

    foreach($categories as $each_category)
        $category_ids[] = $each_category->term_id;
		
    $args = array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'orderby'=> 'desc',
        'showposts' => 5,
        'ignore_sticky_posts' => 0
    );

$query = new WP_Query($args);

    if( $query->have_posts() ) {
        echo '<div class="related">ARTICOLI CORRELATI ALLA CATEGORIA';?>
		<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
	echo '<h4><a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}?>
        <?php echo '</h4><ul>';

        while ($query->have_posts()) {
            $query->the_post();
        ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to: <?php the_title_attribute(); ?>">
            <?php the_title(); ?></a> (<?php the_time('j-m-Y') ?>) </li>
        <?php
        }
        echo '</ul>';
        echo '</div>';
    }
}
wp_reset_query();
?>


<?php
$related_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  $tag_ids = array();
  foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  $args=array(
    'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
    'posts_per_page'=>5);
$related_query = new wp_query( $args );
if( $related_query->have_posts() ) {

   echo '<div class="related">ARTICOLI COLLEGATI AL TAG';?>
		<?php 	echo '<h4><a href="' . esc_url( get_tag_link( $tags[0]->term_id ) ) . '">' . esc_html( $tags[0]->name ) . '</a>';;?>
        <?php echo '</h4><ul>';
  while( $related_query->have_posts() ) { $related_query->the_post(); ?>
    <li>
    <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> (<?php the_time('j-m-Y') ?>) </li>
  <?php } echo '</ul></div>';
  }
} else {
    echo '<div class="related">NON CI SONO TAG</div>';
}
$post = $related_post;
wp_reset_query();
?>
</div>
    <?php get_sidebar(); ?>
	 </aside>
  <?php } ?>

<?php get_footer(); ?>
