<?php
/**
 * The template for displaying Portfolio.
 *
 * @package Trade_Line
 */

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
	</header><!-- .entry-header -->
	<?php
		$arguments = array(
			'type'       => 'jetpack-portfolio',
			'taxonomy'   => 'jetpack-portfolio-type',
			'pad_counts' => false,
		);
		$categories = get_categories( $arguments );
	?>
	<div class="portfolio-main-wrapper">
		<?php
		if ( ! empty( $categories ) ) : ?>

			<div class="portfolio-filter">
				<a href="#"  data-filter="*" class="current"><?php _e( 'All', 'trade-line-pro' ); ?></a>
				<?php foreach ( $categories as $category ) { ?>
				<a data-filter=".<?php echo esc_attr( $category->slug ); ?>" href="#"><?php echo esc_html( $category->name ); ?></a>
				<?php } ?>
			</div>

		<?php endif; ?>
		<div class="portfolio-container">
			<?php
				$args = array(
					'post_type'     => 'jetpack-portfolio',
					'post_status'   => 'publish',
					'posts_per_page' => -1,
				);
				// The Query.
				$the_query = new WP_Query( $args );
			?>
			<?php
			// The Loop.
			if ( $the_query->have_posts() ) {
				while ( $the_query->have_posts() ) {
					$the_query->the_post();

					$terms = get_the_terms( $post->ID, 'jetpack-portfolio-type' );
					$filters = '';
					if ( $terms && ! is_wp_error( $terms ) ) {
						$term_links = array();
						foreach ( $terms as $term ) {
							$term_links[] = $term->slug;
						}
						$filters = join( ' ', $term_links );
					}
					?>
					<div class="portfolio-item <?php echo esc_attr( $filters ); ?>">
						<div class="item-wrapper">
							<h3 class="portfolio-item-title"><a href="<?php the_permalink(); ?>" title="" target="_self"><?php the_title(); ?></a></h3>
							<?php
							if ( has_post_thumbnail() ) {
							?>
								<a href="<?php the_permalink(); ?>" title="" target="_self"><?php the_post_thumbnail( 'medium' ); ?></a>
							<?php
							}
							?>
						</div>
					</div>
					<?php
				} // End while loop.

				// Reset post data.
				wp_reset_postdata();
			}
			?>
		</div> <!-- .portfolio-container -->
	</div>
</article>
