<?php
/**
 * Template Name: Suche
 */

 get_header() ?>

<?php
  $excluded_types = array('page', 'attachment', 'slide');
  $post_type_names = get_post_types(array(
    'public' => true,
  ));
  $post_type_names = array_diff_key($post_type_names, array_flip($excluded_types));

  $query_types = get_query_var('post_type');
  if ( empty($query_types) ) {
    $query_types = array();
  } else if ( $query_types === 'any' ) {
    $query_types = array('post', 'kurs', 'download');
  }
?>

  <section id="content" class="articles wrapper container">
  <?php if ( have_posts() ) { ?>
    <header id="post-<?php the_ID() ?>" <?php post_class('section-header'); ?>>
      <h1 class="title"><?php printf(__( 'Search Results for:', 'sandbox' )); ?> <strong><?php the_search_query(); ?></strong></h1>
      <div class="content">
        <form role="search" method="get" class="search-form" action="<?php echo esc_url(home_url('/')); ?>">
          <label>
            <span class="screen-reader-text"><?php echo _x('Search for:', 'label', 'sandbox'); ?></span>
            <input type="search" class="search-field" placeholder="<?php echo esc_attr_x('Search &hellip;', 'placeholder', 'sandbox'); ?>" value="<?php echo get_search_query(); ?>" name="s" />
          </label>
          <button type="submit" class="search-submit"><?php echo esc_attr_x( 'Search', 'submit button', 'sandbox'); ?></button>
        <?php if ( is_search() && !empty($post_type_names) ) { ?>
          <div class="select-types">
          <?php foreach ( $post_type_names as $post_type_name ) { ?>
            <?php $post_type = get_post_type_object($post_type_name); ?>
            <div class="selection selection-<?php echo $post_type_name; ?>">
              <input type="checkbox" name="post_type[]" value="<?php echo $post_type_name; ?>" <?php if ( in_array($post_type_name, $query_types) ) { echo 'checked="checked"'; } ?> />
              <label><?php echo $post_type->label; ?></label>
            </div>
          <?php } ?>
          </div>
        <?php } ?>
        </form>
        <?php
          /*
          * @Sidebar Content
          */
          get_sidebar('meta'); ?>
      </div>
    </header><!-- .post -->
  <?php while ( have_posts() ) : the_post() ?>

    <?php
      $customPostsMeta = get_post_custom();
      $useCustomMoreText = ( !empty($customPostsMeta["_custom_more_text"]) && strlen($customPostsMeta["_custom_more_text"][0]) > 0 );
      $moreText = $useCustomMoreText ? $customPostsMeta["_custom_more_text"][0] : __('Weiterlesen', 'sandbox');
      // echo var_dump(!empty($customPostsMeta["_custom_more_text"]));
    ?>

    <article id="post-<?php the_ID() ?>" <?php post_class(); ?>>
      <div class="inner clearfix">
        <?php if ( has_post_thumbnail() ) { ?>
          <a href="<?php the_permalink() ?>" class="post-thumbnail"><?php the_post_thumbnail('thumbnail-full'); ?></a>
        <?php } ?>
        <h2 class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </div>
    </article><!-- .post -->

  <?php endwhile; ?>

	  <?php if ( is_paged() ) { ?>

      <?php if ( function_exists('wp_pagenavi') ) {

      	wp_pagenavi(); // http://wordpress.org/plugins/wp-pagenavi/

      } else {

      ?>

		  <nav id="nav-below" class="navigation">
			  <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&laquo;</span> Older results', 'sandbox' ) ) ?></div>
			  <div class="nav-next"><?php previous_posts_link( __( 'Newer results <span class="meta-nav">&raquo;</span>', 'sandbox' ) ) ?></div>
		  </nav>

      <?php } ?>

    <?php } ?>

<?php } else { ?>

  <article id="content" class="post no-results not-found wrapper container">
    <div class="inner clearfix">
      <h1 class="title"><?php printf(__( 'Search Results for:', 'sandbox' )); ?> <strong><?php the_search_query(); ?></strong></h1>
      <div class="content">
        <form role="search" method="get" class="search-form" action="<?php echo esc_url(home_url('/')); ?>">
          <label>
            <span class="screen-reader-text"><?php echo _x('Search for:', 'label', 'sandbox'); ?></span>
            <input type="search" class="search-field" placeholder="<?php echo esc_attr_x('Search &hellip;', 'placeholder', 'sandbox'); ?>" value="<?php echo get_search_query(); ?>" name="s" />
          </label>
          <button type="submit" class="search-submit"><?php echo esc_attr_x( 'Search', 'submit button', 'sandbox'); ?></button>
        <?php if ( is_search() && !empty($post_type_names) ) { ?>
          <div class="select-types">
          <?php foreach ( $post_type_names as $post_type_name ) { ?>
            <?php $post_type = get_post_type_object($post_type_name); ?>
            <div class="selection selection-<?php echo $post_type_name; ?>">
              <input type="checkbox" name="post_type[]" value="<?php echo $post_type_name; ?>" <?php if ( in_array($post_type_name, $query_types) ) { echo 'checked="checked"'; } ?> />
              <label><?php echo $post_type->label; ?></label>
            </div>
          <?php } ?>
          </div>
        <?php } ?>
        </form>
        <h2><?php printf(__( 'Not Found', 'sandbox' )); ?></h2>
			  <p><?php printf(__('Apologies, but we were unable to find what you were looking for. Perhaps  searching will help.', 'sandbox')); ?></p>
			  <p><a href="<?php echo esc_url( home_url() ); ?>"><?php printf(__('Go to the homepage', 'sandbox')); // 'zur Startseite gehen' ?></a></p>
      </div>
    </div>
  </article><!-- .post -->

<?php } ?>
  </section>

<?php get_footer() ?>
