<?php
function portfolio($atts)
{
    extract(shortcode_atts(array(
        'number'           	=> '3',
        'type'           	=> ''
    ), $atts));
    
    $return_portfolio = '';
	if($type == 'style1')
    {
    $pro = '';
        $filter_cat = array(
            'orderby'       => 'name',
            'order'         => 'ASC', 
            'hide_empty'    => 0,
            'hierarchical'  => 1,
            'taxonomy'      => 'portfolio_cat'
        );
        $categories = get_categories( $filter_cat );
        $pro .= '<div class="container"><div id="filters" class="portfolio-filters"><ul class="clearfix">';
        $pro .= '<li><a id="all" class="active" href="#" data-filter="*">All</a></li>';
        foreach($categories as $cat)
        {
            $pro .= '
			<li><a href="#" data-filter=".'.esc_attr($cat->slug).'">'.esc_attr($cat->cat_name).'</a></li>';
        }
        $pro .= '</ul></div></div>';
        
        $filter = array(
            'post_type'     => array('portfolio'),
            'post_status'   => array('publish'),
            'orderby'       => 'ID',
            'order'         => 'ASC',
            'posts_per_page'=> $number
        );
        query_posts($filter);
        if(have_posts())
        {
            $pro .= '<div id="work-wrap" class="style-1">';
            $i = 1;
            
            while(have_posts()): the_post();
                $ida = get_post_thumbnail_id( get_the_ID() );
				$width = get_post_meta(get_the_ID(), 'my_meta_box_text', TRUE);
                if(has_post_thumbnail())
                {
                    $thumgSmall = get_the_post_thumbnail(get_the_ID(), 'port_thumb');
                }
                else
                {
                    $thumgSmall = '<img alt="" src="http://placehold.it/315x315/cccccc&amp;text=IMAGE" alt="Portfolio IMage"/>';
                }
                
                $termsObj = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				if ( $termsObj !=0 ) :
					//We need only the slug of this/these term(s) so looping through the result to grab only the slug value and store it in a new array
					$terms = array();
					foreach( $termsObj as $obj ){
						$terms[] = $obj->slug;
					}
					//Join or implode the new array to a string and then put it in the class attribute of the portoflio item
					$termsString = join( ' ', $terms);
				endif;
				$termsn = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				$te = '';
				foreach($termsn as $termn)
				{
					$te .= ''.esc_attr($termn->name).'<span>/</span>';
				}
				$pro .= '
				<div class="work-item set-width '.$width.' '.$termsString.'">
					<div class="work-image">
						<img src="'.wp_get_attachment_url($ida).'" alt="Portfolio Image"/>
					</div>
					<a href="'.get_the_permalink().'">
						<div class="caption">
							<div class="tb">

								<div class="tb-cell">
									<h2>'.get_the_title().'</h2>
									<p>'.$te.'</p>
								</div>
								
							</div>
						</div>
					</a>
				</div>';
                $i++;
            endwhile;
            $pro .= '</div>';
        }
        else
        {
            $pro .= '<div class="row">';
            $pro .= '<h1 class="text-center">Insert Some Portfolio First</h1>';
            $pro .= '</div>';
        }
		wp_reset_query();
		
	// Style 2 Start	
	
    }
    elseif($type == 'style2')
    {
	
    $pro = '';
        $filter_cat = array(
            'orderby'       => 'name',
            'order'         => 'ASC', 
            'hide_empty'    => 0,
            'hierarchical'  => 1,
            'taxonomy'      => 'portfolio_cat'
        );
        $categories = get_categories( $filter_cat );
        $pro .= '<div id="filters" class="portfolio-filters"><ul class="clearfix">';
        $pro .= '<li><a id="all" class="active" href="#" data-filter="*">All</a></li>';
        foreach($categories as $cat)
        {
            $pro .= '
			<li><a href="#" data-filter=".'.esc_attr($cat->slug).'">'.esc_attr($cat->cat_name).'</a></li>';
        }
        $pro .= '</ul></div>';
        
        $filter = array(
            'post_type'     => array('portfolio'),
            'post_status'   => array('publish'),
            'orderby'       => 'ID',
            'order'         => 'ASC',
            'posts_per_page'=> $number
        );
        query_posts($filter);
        if(have_posts())
        {
            $pro .= '<div id="work-wrap" class="style-2">';
            $i = 1;
            
            while(have_posts()): the_post();
                $ida = get_post_thumbnail_id( get_the_ID() );
				$width = get_post_meta(get_the_ID(), 'my_meta_box_text', TRUE);
                if(has_post_thumbnail())
                {
                    $thumgSmall = get_the_post_thumbnail(get_the_ID(), 'port_thumb');
                }
                else
                {
                    $thumgSmall = '<img alt="" src="http://placehold.it/315x315/cccccc&amp;text=IMAGE" alt="Portfolio IMage"/>';
                }
                $termsObj = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				if ( $termsObj && !is_wp_error( $termsObj ) ) :
					//We need only the slug of this/these term(s) so looping through the result to grab only the slug value and store it in a new array
					$terms = array();
					foreach( $termsObj as $obj ){
						$terms[] = $obj->slug;
					}
					//Join or implode the new array to a string and then put it in the class attribute of the portoflio item
					$termsString = join( ' ', $terms);
				endif;
				$termsn = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				$te = '';
				foreach($termsn as $termn)
				{
					$te .= ''.esc_attr($termn->name).'<span>/</span>';
				}
				$pro .= '
				<div class="work-item set-width '.$width.' '.$termsString.'">
					<div class="work-image">
						<img src="'.wp_get_attachment_url($ida).'" alt="Portfolio Image"/>
					</div>
					<a href="'.get_the_permalink().'">
						<div class="caption">
							<div class="tb">

								<div class="tb-cell">
									<h2>'.get_the_title().'</h2>
									<p>'.$te.'</p>
								</div>
								
							</div>
						</div>
					</a>
				</div>';
                $i++;
            endwhile;
            $pro .= '</div>';
        }
        else
        {
            $pro .= '<div class="row">';
            $pro .= '<h1 class="text-center">Insert Some Portfolio First</h1>';
            $pro .= '</div>';
        }
		wp_reset_query();	
	// Style 3 Start	
	
    }
    elseif($type == 'style3')
    {
	
    $pro = '';
        $filter_cat = array(
            'orderby'       => 'name',
            'order'         => 'ASC', 
            'hide_empty'    => 0,
            'hierarchical'  => 1,
            'taxonomy'      => 'portfolio_cat'
        );
        $categories = get_categories( $filter_cat );
        $pro .= '<div id="filters" class="portfolio-filters"><ul class="clearfix">';
        $pro .= '<li><a id="all" class="active" href="#" data-filter="*">All</a></li>';
        foreach($categories as $cat)
        {
            $pro .= '
			<li><a href="#" data-filter=".'.esc_attr($cat->slug).'">'.esc_attr($cat->cat_name).'</a></li>';
        }
        $pro .= '</ul></div>';
        
        $filter = array(
            'post_type'     => array('portfolio'),
            'post_status'   => array('publish'),
            'orderby'       => 'ID',
            'order'         => 'ASC',
            'posts_per_page'=> $number
        );
        query_posts($filter);
        if(have_posts())
        {
            $pro .= '<div id="work-wrap" class="style-3">';
            $i = 1;
            
            while(have_posts()): the_post();
                $ida = get_post_thumbnail_id( get_the_ID() );
				$width = get_post_meta(get_the_ID(), 'my_meta_box_text', TRUE);
                if(has_post_thumbnail())
                {
                    $thumgSmall = get_the_post_thumbnail(get_the_ID(), 'port_thumb');
                }
                else
                {
                    $thumgSmall = '<img alt="" src="http://placehold.it/315x315/cccccc&amp;text=IMAGE" alt="Portfolio IMage"/>';
                }
                $termsObj = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				if ( $termsObj && !is_wp_error( $termsObj ) ) :
					//We need only the slug of this/these term(s) so looping through the result to grab only the slug value and store it in a new array
					$terms = array();
					foreach( $termsObj as $obj ){
						$terms[] = $obj->slug;
					}
					//Join or implode the new array to a string and then put it in the class attribute of the portoflio item
					$termsString = join( ' ', $terms);
				endif;
				$termsn = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				$te = '';
				foreach($termsn as $termn)
				{
					$te .= ''.esc_attr($termn->name).'<span>/</span>';
				}
				$pro .= '
				<div class="work-item set-width '.$width.' '.$termsString.'">
					<div class="work-image">
						<img src="'.wp_get_attachment_url($ida).'" alt="Portfolio Image"/>
					</div>
					<a href="'.get_the_permalink().'">
						<div class="caption">
							<div class="tb">

								<div class="tb-cell">
									<h2>'.get_the_title().'</h2>
									<p>'.$te.'</p>
								</div>
								
							</div>
						</div>
					</a>
				</div>';
                $i++;
            endwhile;
            $pro .= '</div>';
        }
        else
        {
            $pro .= '<div class="row">';
            $pro .= '<h1 class="text-center">Insert Some Portfolio First</h1>';
            $pro .= '</div>';
        }
		wp_reset_query();	
	// Style 4 Start	
	
    }
    elseif($type == 'style4')
    {
	
    $pro = '';
        $filter_cat = array(
            'orderby'       => 'name',
            'order'         => 'ASC', 
            'hide_empty'    => 0,
            'hierarchical'  => 1,
            'taxonomy'      => 'portfolio_cat'
        );
        $categories = get_categories( $filter_cat );
        $pro .= '<div id="filters" class="portfolio-filters"><ul class="clearfix">';
        $pro .= '<li><a id="all" class="active" href="#" data-filter="*">All</a></li>';
        foreach($categories as $cat)
        {
            $pro .= '
			<li><a href="#" data-filter=".'.esc_attr($cat->slug).'">'.esc_attr($cat->cat_name).'</a></li>';
        }
        $pro .= '</ul></div>';
        
        $filter = array(
            'post_type'     => array('portfolio'),
            'post_status'   => array('publish'),
            'orderby'       => 'ID',
            'order'         => 'ASC',
            'posts_per_page'=> $number
        );
        query_posts($filter);
        if(have_posts())
        {
            $pro .= '<div id="work-wrap" class="style-4">';
            $i = 1;
            
            while(have_posts()): the_post();
                $ida = get_post_thumbnail_id( get_the_ID() );
				$width = get_post_meta(get_the_ID(), 'my_meta_box_text', TRUE);
                if(has_post_thumbnail())
                {
                    $thumgSmall = get_the_post_thumbnail(get_the_ID(), 'port_thumb');
                }
                else
                {
                    $thumgSmall = '<img alt="" src="http://placehold.it/315x315/cccccc&amp;text=IMAGE" alt="Portfolio IMage"/>';
                }
                $termsObj = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				if ( $termsObj && !is_wp_error( $termsObj ) ) :
					//We need only the slug of this/these term(s) so looping through the result to grab only the slug value and store it in a new array
					$terms = array();
					foreach( $termsObj as $obj ){
						$terms[] = $obj->slug;
					}
					//Join or implode the new array to a string and then put it in the class attribute of the portoflio item
					$termsString = join( ' ', $terms);
				endif;
				$termsn = wp_get_post_terms( get_the_ID(), 'portfolio_cat');
				$te = '';
				foreach($termsn as $termn)
				{
					$te .= ''.esc_attr($termn->name).'<span>/</span>';
				}
				$pro .= '
				<div class="work-item set-width '.$width.' '.$termsString.'">
					<div class="work-image">
						<img src="'.wp_get_attachment_url($ida).'" alt="Portfolio Image"/>
					</div>
					<a href="'.get_the_permalink().'">
						<div class="caption">
							<div class="tb">

								<div class="tb-cell">
									<h2>'.get_the_title().'</h2>
									<p>'.$te.'</p>
								</div>
								
							</div>
						</div>
					</a>
				</div>';
                $i++;
            endwhile;
            $pro .= '</div>';
        }
        else
        {
            $pro .= '<div class="row">';
            $pro .= '<h1 class="text-center">Insert Some Portfolio First</h1>';
            $pro .= '</div>';
        }
		wp_reset_query();	
	}
    return $pro;
}
add_shortcode( "rms-portfolio", "portfolio" );