<?php
/**
 * The slideshow hook for our theme.
 *
 * This is the template that displays slider of the theme
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package OMag
 */

if ( ! function_exists( 'omag_home_slideshow' ) ) :

  function omag_home_slideshow(){

    $theme_options  = omag_theme_options();


    if(!empty( $theme_options['slider_cat'] )){

      $slider_args = array( 
        'cat'             => absint( $theme_options['slider_cat'] ), 
        'post_status'     => 'publish', 
        'posts_per_page'  => 4,
        'ignore_sticky_posts' => 1,
      );

    } else{

      $slider_args = array( 'post_status' => 'publish', 'posts_per_page' => 4, 'ignore_sticky_posts' => 1, );

    } 

    $slider_query = new WP_Query( $slider_args ); 

    if ( $slider_query->have_posts() ) {

      ?>

      <div class="col-lg-6">
        <div id="news-banner" class="owl-carousel owl-theme">

          <?php

          while ( $slider_query->have_posts() ) : $slider_query->the_post(); 

            $image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );

            ?>

            <div class="item">
              <a href="<?php the_permalink(); ?>">
                <?php if(!empty($image_url)) { ?>
                    <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
                  <?php } else { ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/images/default.jpg" alt="<?php the_title(); ?>">
                <?php } ?>
              </a>
              <div class="banner-text">
                <div class="banner-text-cover">
                  <div class="tags-button">
                    <?php if( 1 === $theme_options['enable_post_tags']) {
                      omag_posts_tag();
                    } ?>
                  </div>
                  <div class="banner-header">
                    <h3>
                      <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                      </a>
                    </h3>
                  </div>
                  <div class="banner-info">

                    <?php
                      if( 1 === $theme_options['enable_post_date']) {
                        omag_posted_on();
                      }
                    ?>

                    </div>
                  </div>
                </div>
              </div>

              <?php 
            endwhile;
            wp_reset_postdata(); 
            ?>

          </div>
        </div>

        <?php
      }

    }
  endif;

add_action( 'omag_slideshow', 'omag_home_slideshow', 10 );
