<?php 

// [gl_blog ][/gl_blog]
function gl_blog_func($atts)
{
    $attr = shortcode_atts(array(
        'number_of_blog' => 1,
        'blog_img' => "yes",
        'image_size' => "thumbnail",
        'blog_date' => "yes",
        'blog_dec' =>  "yes",
        'cate_id' =>  "",
        'blog_readmore_btn' =>  "yes",
    ), $atts);
	$cate_id = $attr["cate_id"]; 
	
	if(empty($cate_id)){
	
		$args = array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $attr["number_of_blog"]);	
	}
	else {
		$args = array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $attr["number_of_blog"] , 'cat' => $attr["cate_id"]);

	}

    $loop = new WP_Query($args);
    $output = "";
    if (!empty($loop->posts)) {
        $output .= '<div class="grid-1 BlogListing">';
        foreach ($loop->posts as $key => $post) {
            $month = date("F", strtotime($post->post_date));
            $pdate = date("j", strtotime($post->post_date));
            $pYear = date("Y", strtotime($post->post_date));
            $thumbnail_id  = get_post_thumbnail_id($post->ID);
            $thumbnail_alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
            $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $attr["image_size"]);

            $portfolio_tags = get_the_terms($post->ID, 'category');
            $output .= '
                    <div class="item-box">
                        <div class="blog-items">
                            <div class="b-item">';
                            $output .= '  <div class="thumb">';
            if (has_post_thumbnail($post->ID)) {
                $output .='<a href="' . get_permalink($post->ID) . '">';
                $output .= '<img src="' . $thumbnail['0'] . '" alt="' . $thumbnail_alt . '" />';
                $output .='</a>';
            } else {
                $output .='<a href="' . get_permalink($post->ID) . '">';
                $output .=  '<img src="' . get_bloginfo('stylesheet_directory')
                    . '/assets/images/no_img_1.png" alt="" />';
                    $output .='</a>';
            }
            $output .= ' </div>';
            $output .= '<div class="blog-content ">';
            $output .= '<div class="blog-cate-outer ">';
            if ($portfolio_tags) {
                $output .= '<div class="portfolio_tags">';
                $tag_count = count($portfolio_tags);
                foreach ($portfolio_tags as $index => $tag) {
                    
                    $output .= '<span  class="case-tag">' . $tag->name . '</span>';
                    // Add the separator (|) if it's not the last tag
                    if ($index < $tag_count - 1) {
                        $output .= ' | ';
                    }
                }
                $output .= '</div>';
            }
          $output .= ' <div class="blog-date ">
                            <span>' . $pdate . '  ' .   $month .  ' ' .   $pYear .  ' </span>
                        </div>';

                        $output .= '</div>';

                       $output .= ' <div class="blog-info">
                            <h4>  <a  href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a> </h4>
                            <div class="content">' . wp_trim_words( $post->post_content, 350, '...' ) . ' </div>
                            <div class="btnOuter">

                            <a class="elementor-button elementor-button-link elementor-size-sm" href="' . get_permalink($post->ID) . '">Read More</a>
                            <a class="elementor-button elementor-button-link elementor-size-sm" href="'.get_site_url() .'/news/">View All</a>

                            </div>
                        </div>
                    </div>
                    </div>
                </div>
            </div>';
        }
    }
    $output .= '</div>';
    // <p>' . $post->post_excerpt . '</p>
    return $output;
}
add_shortcode('gl_blog', 'gl_blog_func');



function gl_recent_post_func($atts)
{
    $attr = shortcode_atts(array(
        'number_of_blog' => 3,
        'blog_img' => "yes",
        'image_size' => "thumbnail",
        'blog_date' => "yes",
        'blog_dec' =>  "yes",
        'cate_id' =>  "",
        'blog_readmore_btn' =>  "yes",
    ), $atts);
	$cate_id = $attr["cate_id"]; 
	
	if(empty($cate_id)){
	
		$args = array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $attr["number_of_blog"]);	
	}
	else {
		$args = array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $attr["number_of_blog"] , 'cat' => $attr["cate_id"]);

	}
    
    $loop = new WP_Query($args);
    $output = "";
    if (!empty($loop->posts)) {
        $output .= '<div class="b-outer grid-1">';
        foreach ($loop->posts as $key => $post) {
            $month = date("F", strtotime($post->post_date));
            $pdate = date("j", strtotime($post->post_date));
            $pYear = date("Y", strtotime($post->post_date));
            $thumbnail_id  = get_post_thumbnail_id($post->ID);
            $thumbnail_alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
            $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $attr["image_size"]);
            $tags = get_the_tags($post->ID);
            $output .= '<div class="blog-recent-items">';
            $output .='<a class="fulllink" href="' . get_permalink($post->ID) . '"></a> 
                            <div class="b-left">
                                <div class="b-thumb">';
            if (has_post_thumbnail($post->ID)) {
                
                $output .= '<img src="' . $thumbnail['0'] . '" alt="' . $thumbnail_alt . '" />';
              
            } else {
                $output .=  '<img src="' . get_bloginfo('stylesheet_directory')
                    . '/assets/images/no_img_1.png" alt="" />';
                 
            }
            $output .= ' </div>';
            $output .= '</div>';
              $output .= '<div class="b-right"> 
                                <div class="b-date ">
                                    <span>' . $pdate . '  ' .   $month .  ' ' .   $pYear .  ' </span>
                                </div> <h4 class="b-title"> ' . $post->post_title . ' </h4>';

               $output .= ' </div> ';
              
               $output .= '   </div>';
                

        }
    }
    $output .= '</div>';
    return $output;
}
add_shortcode('gl_blog_recent', 'gl_recent_post_func');
