<?php 
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
 } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
 } else {
    $paged = 1;
 }
$query_args = array(
    'post_type' => 'post',
    'posts_per_page' => $settings['posts_per_page'],
    'paged'     => $paged
);
if (! empty( $settings['posts_categories'] )) {
    $query_args['category_name'] = implode(',', $settings['posts_categories']);
}        
if ( ! empty( $settings['exclude'] ) ) {				
    if ( ! is_array( $settings['exclude'] ) )
        $exclude = explode( ',', $settings['exclude'] );

    $query_args['post__not_in'] = $exclude;
}
$query_args['orderby'] = $settings['order_by'];
$query_args['order'] = $settings['order'];	
$query = new WP_Query( $query_args );

 
 ?>
<?php $count = 1; while ( $query->have_posts() ) : $query->the_post();?>
<?php 
$get_id_post_thumbnail = get_post_thumbnail_id();
$featured_image = sprintf('<img src="%s" alt="image">', \Elementor\Group_Control_Image_Size::get_attachment_image_src( $get_id_post_thumbnail, 'thumbnail', $settings ));  

?>
<div class="item <?php if($settings['style_layout'] == 'style-layout2') {
     echo $last_post = ($count == $settings['posts_per_page']) ? 'last-post' : '';
    }else {
        $last_post = '';
    }
?>

" >
    <div class="entry blog-post "> 
        <?php if ( $settings['show_image'] == 'yes' && $last_post != 'last-post' ): ?>                                    
        <div class="featured-post">
            <a href="<?php echo esc_url( get_permalink() ) ?>">
                <?php echo sprintf('%s',$featured_image); ?>
            </a>            
        </div>
        <?php endif; ?>
        
        <div class="content">
            <?php if ( $last_post == 'last-post' ): ?> 
                <div class="icon">
                    <?php echo \Elementor\Addon_Elementor_Icon_manager_norstar::render_icon( $settings['layout_icon'], [ 'aria-hidden' => 'true' ] );?>
                </div>
            <?php endif; ?>   
        <?php if ( $settings['show_title'] == 'yes' ): ?>
                <h2 class="title"><a href="<?php echo esc_url( get_the_permalink() ); ?>" title="<?php echo esc_attr( get_the_title() ); ?>"><?php echo get_the_title(); ?></a></h2>
            <?php endif; ?>
            <?php if ( $settings['show_meta'] == 'yes' ): ?> 
                <?php
                $archive_year  = get_the_time('Y'); 
                $archive_month = get_the_time('m'); 
                $archive_day   = get_the_time('d');
                ?>
                <div class="meta-post">
                    <span class="post-meta post-meta-time">
                    <a href="<?php echo get_day_link($archive_year, $archive_month, $archive_day); ?>">
                    <?php if ( $settings['date_icon'] != '' ): ?>
                            <?php echo \Elementor\Addon_Elementor_Icon_manager_norstar::render_icon( $settings['date_icon'], [ 'aria-hidden' => 'true' ] );?>
                        <?php endif; ?>
                    <?php echo get_the_date(); ?>
    
                    </a></span>
                     <span class="post-meta-author">
                         <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><i class='icon-norstar-user'></i>  <?php echo esc_html__('Posted by: ', 'themesflat-core').'<span>'.get_the_author().'</span>'; ?></a>
                     </span>
                </div>
            <?php endif; ?> 

            <?php if ( $last_post == 'last-post' ): ?> 
                <h4 class="author"><?php echo esc_attr($settings['layout_author']); ?></h4>
            <?php endif; ?> 

            <?php if ( $settings['show_excerpt_2'] == 'yes' ): ?>
                <div class="description"><?php echo wp_trim_words( get_the_content(), $settings['excerpt_lenght'], '' ); ?></div>
            <?php endif; ?>  

            <?php if ( $settings['show_button'] == 'yes' && $settings['button_text'] != '' ): ?>
                <div class="tf-button-container">
                <a href="<?php echo esc_url( get_permalink() ) ?>" class="tf-button">
                    <span><?php echo esc_attr( $settings['button_text'] ); ?></span>
                    <?php echo \Elementor\Addon_Elementor_Icon_manager_norstar::render_icon( $settings['post_icon_readmore'], [ 'aria-hidden' => 'true' ] );?>
                </a>
                </div>
            <?php endif; ?> 
        </div>                      
    </div>
</div>
<?php $count++; endwhile;  ?>