<?php
/**
 * The main archive template file
 *
 * @package kadence
 */

namespace Kadence;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

get_header();

kadence()->print_styles( 'kadence-content' );
/**
 * Hook for main archive content.
 */
$slug = ( is_search() && ! is_post_type_archive( 'product' ) ? 'search' : get_post_type() );
if ( empty( $slug ) ) {
	$queried_object = get_queried_object();
	if ( property_exists( $queried_object, 'taxonomy' ) ) {
		$current_tax = get_taxonomy( $queried_object->taxonomy );
		if ( property_exists( $current_tax, 'object_type' ) ) {
			$post_types = $current_tax->object_type;
			$slug = $post_types[0];
		}
	}
}


$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$set_tags = get_field('tag',$current_term->taxonomy.'_'.$current_term->term_id);
$set_type = get_field('type',$current_term->taxonomy.'_'.$current_term->term_id);

$image = get_term_meta($current_term->term_id, 'kwp-tax-image-id', true);
$image = empty($image) ? '' : '<div class="bw-resource-image">' . wp_get_attachment_image($image) . '</div>';
?>
<div id="primary" class="content-area">
	<div class="content-container site-container">
		<main id="main" class="site-main" role="main">
			<div class="content-wrap">
				<article class="entry content-bg single-entry bw-resource-single-entry">
					<div class="entry-content-wrap">
						<header class="bw-resource-header">
							<?php echo $image; ?>
							<div class="bw-title-column">
								<div class="bw-resource-tax">
									<ul>
										<?php
											$type_link = get_term_link( $set_type );
											if ( !is_wp_error( $type_link ) ) {
												echo '<li><a href="' . esc_url( $type_link ) . '">' . $set_type->name . '</a></li>';
											}
										?>
									</ul>
								</div>
								<?php the_archive_title( '<h1 class="page-title archive-title">', '</h1>' ); ?>
							</div>
						</header>
						<div class="entry-content single-content">
							<? the_archive_description( '<div class="archive-description">', '</div>' ); ?>
							<?php if ( have_posts() ) : ?>
								<div>
									<?php
									echo '<div class="bw-media-list">';
										while ( have_posts() ) {
											the_post();
											$media = get_field('media');
											if ( ! is_array($media) ) $media = array();
											get_template_part( 'template-parts/resource-content/entry_media', get_post_type(), array('media' => $media, 'type' => 'set' ) );
										
										}
									echo '</div>';
									?>
								</div>
							<?php endif; ?>
						</div>
						<footer>
							<?php do_action( 'kadence_header_social' ); ?>
							<?php get_template_part( 'template-parts/resource-content/entry_tags', get_post_type(), array('tags' => $set_tags ) ); ?>
						</footer>
					</div>
				</article>
			</div>
		</main><!-- #main -->
		<?php
		get_sidebar();
		?>
	</div>
</div>
<?php 
get_footer();
