<?php
global $single_portfolio;
$args = '';
if( ! isset($_GET['ajax']) ) {
    get_header();
    $args = array(
        'post_type' => 'portfolio',
        'p' => get_the_ID()
    );
} else {
    require_once('../../../wp-load.php');
    $args = array(
        'post_type' => 'portfolio',
        'p' => $_GET['id']
    );
    echo '<div class="container portfolio-single"><div class="row"><div class="col-md-12">'; 
}

$portfolio_posts = new WP_Query( $args ); 
while($portfolio_posts->have_posts()) : 
    $portfolio_posts->the_post(); 
    $id = get_the_ID();
    if(get_post_meta($id, $key ='gallery_images', $single = true )) {
        $gallery_images = explode(",",get_post_meta($id, $key ='gallery_images', $single = true )); 
        
        foreach($gallery_images as $key=>$item) {
            if($item == '') {
                unset($gallery_images[$key]);
            }
        } 
        $header_media = "<div id='post-images' class='carousel slide post-images' data-ride='carousel'>";
        $header_media .= "<div class='carousel-inner'>";
        $j=0;
        foreach($gallery_images as $item) {
            $image_src = wp_get_attachment_image_src($item, "full"); 
            $image_title = get_the_title($item); 
            if($j==0) {
                $active_class = " active";
            } else {
                $active_class = "";
            }
            $header_media .= "<div class='item$active_class'>";
            $header_media .= "<img alt='".$image_title."'  src='".$image_src[0]."'>";
            $header_media .= "</div>";
            $j++;
        }
        $header_media .= "</div>";
        if(count($gallery_images) > 1) {
            $header_media .= "<ol class='carousel-indicators'>";
            for($i=0;$i<count($gallery_images);$i++) {
                if($i==0) {
                    $active_class = "active";
                } else {
                    $active_class = "";
                }
                $header_media .= "<li data-target='#post-images' data-slide-to='".$i."' class='".$active_class."'></li>";
            }
            $header_media .= "</ol>";
        }
        if(count($gallery_images) > 1) {
            $header_media .= "<a class='left carousel-control' href='#post-images' data-slide='prev' role='button'>
                                <i class='fa fa-chevron-left'></i>
                              </a>
                              <a class='right carousel-control' href='#post-images' data-slide='next' role='button'>
                                <i class='fa fa-chevron-right'></i>
                              </a>";
        }
        $header_media .= "</div>";
    }
    elseif(has_post_thumbnail($id)) { 
        $header_media = get_the_post_thumbnail($id, "full");
    }
    elseif(get_post_meta($id, $key ='anps_featured_video', $single = true )) { 
        $header_media = do_shortcode(get_post_meta($id, $key ='anps_featured_video', $single = true ));
    }
    else { 
        $header_media = "";
    }
?>
<?php if( !isset($_GET['ajax']) ): $single_portfolio = "true"; ?>
<section class="style-2 portfolio-single">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <?php echo $header_media; ?>
                <h3><?php the_title(); ?></h3>
                <?php the_content(); ?>
            </div>
        </div>
    </div>
</section>
<section class="load-more text-center">
    <div class="container">
        <div class="row">
            <?php previous_post_link('%link', '<i class="fa fa-chevron-left"></i> '.__("previous",ANPS_TEMPLATE_LANG)); ?>
            <a href="<?php echo esc_url(home_url("/")); ?>" class="btn btn-md secondary">
                <i class="fa fa-home"></i><?php _e("home",ANPS_TEMPLATE_LANG); ?>
            </a>
            <?php //if(next_post_link()){
                    next_post_link('%link', __('next', ANPS_TEMPLATE_LANG)." <i class='fa fa-chevron-right'></i>");
                    //}
                ?>
        </div>
    </div>
</section>
<?php else: ?>
    <?php echo $header_media; ?>
    <h3><?php the_title(); ?></h3>
    <?php the_content(); ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
if( ! isset($_GET['ajax']) ) {
    get_footer(); 
} else {
    echo '</div></div></div>'; 
}
?>