Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

WP_Query arguments based on if statement

Hi guys, Im struggling to tackle this issue, I need to add ''nieuwssoort' => 'algemeen' to the WP_query arguments but ONLY if the post-type archive is nieuws

Right now I have this code:

if ( is_post_type_archive( 'projecten' )){
    $posttype = "projecten";
    );
  } elseif ( is_post_type_archive( 'newparc' )){
    $posttype = "newparc";
    );
  } elseif ( is_post_type_archive( 'nieuws' )){
    $posttype = "nieuws";
  }

to determine the posttype in two queries later down the page.

for the second query, I need something like this but it does not work:

if ( is_post_type_archive( 'projecten' ) || is_post_type_archive( 'newparc' ) ) {
  $args = array( 
  'post_type'       => $posttype,
  'posts_per_page'  =>-1,
  );
} elseif ( is_post_type_archive( 'nieuws' )){
  $args = array( 
  'post_type'       => $posttype,
  'posts_per_page'  =>-1,
  'nieuwssoort'     => 'algemeen'
  );
}

Here is my full code :

<?php get_header(); ?>
<?php include (TEMPLATEPATH . '/includes/variables.php'); ?>
<!-- Carousel -->
  <?php
  if ( is_post_type_archive( 'projecten' )){
    $posttype = "projecten";
    );
  } elseif ( is_post_type_archive( 'newparc' )){
    $posttype = "newparc";
    );
  } elseif ( is_post_type_archive( 'nieuws' )){
    $posttype = "nieuws";
  } 

    $args = array(
      'post_type'     => 'fotoslider',
      'orderby'       => 'rand',
      'posts_per_page'=>1,
      'meta_query'    => array(
        'relation'      => 'OR',
        array(
          'key'       => 'weergave',
          'value'     => $posttype,
          'compare'   => 'LIKE'
        ),
      )
    );
    $the_query = new WP_Query( $args );

  ?>

<div id="homecarousel" class="carousel slide" data-ride="carousel">
  <style type="js">$('#homecarousel').carousel({pause: 'hover';})</style>



  <!-- Wrapper for slides -->
  <div class="carousel-inner">

    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div class="item <?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>">
        <div class="fill" style="background-image:url('<?php the_field("slider_foto"); ?>')"></div>
        <div class="carousel-caption">
            <h1><?php echo $posttype; ?></h1>
        </div>
    </div>

    <?php endwhile; endif; ?>

  </div>

</div>
<!-- End Carousel -->

<!-- Page Content -->
<?php
if ( is_post_type_archive( 'projecten' ) || is_post_type_archive( 'newparc' ) ) {
  $args = array( 
  'post_type'       => $posttype,
  'posts_per_page'  =>-1,
  );
} elseif ( is_post_type_archive( 'nieuws' )){
  $args = array( 
  'post_type'       => $posttype,
  'posts_per_page'  =>-1,
  'nieuwssoort'     => 'algemeen'
  );
}
  $the_query = new WP_Query( $args );
?>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

  <div class="content-section-<?php if( $the_query->current_post%2 == 1 ){ echo 'b';}else{ echo 'a';} ?>">
  <div class="container">
    <div class="row">
        <div class="col-lg-5 <?php if( $the_query->current_post%2 == 1 ){ echo 'col-lg-offset-1 col-sm-push-6 ';} ?>col-sm-6 lead-parent">
            <hr class="hidden-xs section-heading-spacer">
            <div class="clearfix"></div>
            <?php if ( is_post_type_archive( 'nieuws' )){ ?><a href="<?php the_permalink() ?>" class="link-no-decoration"><?php }?><h2 class="section-heading"><?php the_field('kop'); ?></h2><?php if ( is_post_type_archive( 'nieuws' )){ ?></a><?php }?>
            <?php if ( is_post_type_archive( 'nieuws' )){
              echo '<p>' . get_field('intro') . '</p>';
            } else {
              the_field('artikel');
            } ?>
            <?php if ( is_post_type_archive( 'nieuws' )){ ?>
              <a href="<?php the_permalink(); ?>"><button class="<?php echo $button; ?>">Bekijk Artikel</button></a>
            <?php }?>
        </div>
        <div class="col-lg-5 <?php if( $the_query->current_post%2 == 1 ){ echo 'col-sm-pull-6';}else{ echo 'col-lg-offset-2';} ?> col-sm-6">
            <a <?php if ( is_post_type_archive( 'nieuws' )){ ?>
                href="<?php the_permalink() ?>";
              <?php } else { ?>
                href="<?php the_field('foto'); ?>";
              <?php } ?> class="foto-wrapper"
              <?php if ( !is_post_type_archive( 'nieuws' )){ 
                echo 'data-gallery'; 
              }?>
            >
              <img class="img-responsive img-rounded" src="<?php the_field('foto'); ?>" alt="<?php the_field('kop'); ?>">
              <?php if ( !$detect->isMobile() ) { ?>
              <?php if ( is_post_type_archive( 'nieuws' )){
                echo '<span class="glyphicon glyphicon-share-alt"></span>';
              } else {
                echo '<span class="glyphicon glyphicon-fullscreen"></span>';
              } ?>
              <?php } ?>
            </a>
        </div>
    </div>
  </div>
</div>

<?php endwhile; else: ?>

<p>Niks te weergeven hier!</p>

<?php endif; ?>

<?php get_footer(); ?>

What am I missing here?

Thanks!

3 Answers

Boris Kamp
Boris Kamp
16,660 Points

I got it! Tried it again with a more logic setup for the $args and now it's working. Here's the code I now use to determine the parameters for the Query:

<?php
  if ( is_post_type_archive( 'nieuws' )){
  $args = array( 
    'post_type'             => $posttype,
    'posts_per_page'        =>-1,
    'nieuwssoort'     => 'algemeen'
    );
  } else {
    $args = array( 
    'post_type'             => $posttype,
    'posts_per_page'        =>-1
    );
  }
  $the_query = new WP_Query( $args );
?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Your logic goes if, elseif, elseif. I think you need to end with an else?

Boris Kamp
Boris Kamp
16,660 Points

Thanks for checking in Zac! thats not the problem if you ask me, I got the first block successfully working on my archive.php to create different bootstrap carousels for each posttype archive. Although, the code will fail if visited from an archive other than the three coded, but it's a small site and Im sure none other will be created so to me this will suffice. What do you think?

Do you think you can help me out out with what's failing my archive.php?

Thanks Zac!