<?php
/**
 * Template Name: Knowledgebase Style FAQ
 * @package S-Corp WordPress Theme
 * @since 1.0
 * @author AJ Clarke : http://wpexplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://wpexplorer.com
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

get_header(); // Loads the header.php template
if (have_posts()) :
	while (have_posts()) : the_post();  // Start loop - don't bother with anything if there isn't a post

	// Display page slider based on meta content
	get_template_part( 'content-page', 'slider' );
		
	// Show featured image
	if( has_post_thumbnail( get_the_ID() ) ) { ?>
		<div id="page-featured-img" class="boxed <?php if ( get_post_meta( get_the_ID(), 'wpex_page_featured_img_overlay', true ) !== '' ) echo 'with-overlay'; ?>">
			<img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ),  wpex_img( 'page_width' ), wpex_img( 'page_height' ), wpex_img( 'page_crop' ) ); ?>" alt="<?php echo the_title(); ?>" />
			<?php
            // Featured Image Overlay
			if ( function_exists('wpex_featured_image_overlay') ) wpex_featured_image_overlay(); ?>
		</div><!-- #page-featured-img -->
	<?php }

	// Show header unless disabled via the custom field "wpex_page_disable_heading"
	if ( get_post_meta( get_the_ID(), 'wpex_page_disable_heading', true) !== 'yes' ) { ?>
        <header id="page-heading">
            <div class="boxed">
                <h1><?php the_title(); ?></h1>
                <?php
                // Yoast Breadcrumbs Support
                if( function_exists('yoast_breadcrumb') ) yoast_breadcrumb('<div id="breadcrumbs"><span class="icon-home"></span>','</div>'); ?>
            </div><!-- /boxed -->
        </header><!-- /page-heading -->
	<?php } ?>

    <div id="kb-page" class="clearfix boxed container">
    
        <div id="kb-page-content" class="entry">
            <?php the_content(); ?>
        </div><!-- #kb-page-content -->
        
        <div id="kb-search">
          <form method="get" class="searchbar" action="<?php echo home_url( '/' ); ?>"><input type="search" name="s" placeholder="<?php _e('Search for answers right here.','wpex'); ?>">
            <input type="hidden" name="post_type" value="faq" />
            </form>
            
            <span class="icon-search"></span>
        </div><!-- #kb-search -->
        
        <div class="entry clearfix">	
            
            <?php
            // Loop through FAQ categories
            $faq_terms = get_terms( 'faq_category','orderby=custom_sort');
            $count=0;
            foreach( $faq_terms as $faq_term ) :
                $count++; ?>
                
                <div class="kb-entry grid-3 <?php if ( $count == '3' ) echo 'remove-margin'; ?>">
                <h2 class="kb-cat-title"><span><?php echo $faq_term->name; ?></span></h2>
                    <ul>   
                        <?php
                        $faqs = new WP_Query(
                            array(
                                'post_type' => 'faq',
                                'posts_per_page' => -1,
                                'no_found_rows' => true,
                                'tax_query' => array(
                                    array(
                                        'taxonomy' => 'faq_category',
                                        'terms' => $faq_term->slug,
                                        'field' => 'slug'
                                    )
                                )
                            )
                        );
                        foreach ( $faqs->posts as $post ) : setup_postdata( $post ); ?>
                            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><span class="icon-circle-blank"></span><?php the_title(); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                </div><!-- .kb-entry -->
                <?php if ( $count == '3' ) {
                    echo '<div class="clear"></div>';
                    $count=0;
                } ?>
                
            <?php endforeach;
            wp_reset_postdata(); ?>
            
        </div><!-- /entry -->
    </div><!-- /container -->

	<?php
	$wpex_page_featured_img_overlay = NULL;
	endwhile;
endif;
get_footer(); // Loads the footer.php file ?>