WordPress.org

Forums

Calling Custom Post Type Taxonomy + Conditional Statements = Can I Simplify? (2 posts)

  1. typecconsulting
    Member
    Posted 1 year ago #

    Hi All,

    So I'm just getting the hang of custom post types and I've created a page template that uses conditional statements to call all custom post types from a certain custom taxonomy depending on the page ID. It works great, but...it's huge and repetitive. I know that there must be a way to shorten/simplify the entire thing, but I cannot figure it out for the life of me. Can anyone help?

    <?php
    /*
    Template Name: Restaurants
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content-background">
       <div class="container">
    	<div id="page-title">
    	    <h1>Restaurants</h1>
    	    <div class="arrow"></div>
    	</div>
       	<section id="content" role="main">
    	   <div class="row">
    		<div class="col-sm-3 col-md-3 left-sidebar">
    		   <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Restaurants Sidebar') ) : ?>
    		   <?php endif; ?>
    		</div>
    	   	<div class="col-sm-9 col-md-9 content-right">
    		<div id="restaurant-listings">
    		    <h2><?php the_title(); ?></h2>
    			<div class="row">
    				<?php if (is_page(8)) {
    				   $my_query = new WP_Query('post_type=restaurant&posts_per_page=-1');
    				   while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    
    				<?php endwhile;  wp_reset_query(); } ?>
    
    				<?php if (is_page(10)) {
    					$args = array(
    					'post_type' => 'restaurant',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'vancouver'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    			<div class="row">
    				<?php if (is_page(19)) {
    					$args = array(
    					'post_type' => 'restaurant',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'toronto'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    			<div class="row">
    
    				<?php if (is_page(16)) {
    					$args = array(
    					'post_type' => 'restaurant',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'victoria'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    			<div class="row">
    				<?php if (is_page(14)) {
    					$args = array(
    					'post_type' => 'restaurant',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'kingston'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    			<div class="row">
    				<?php if (is_page(165)) {
    					$args = array(
    					'post_type' => 'restaurant',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'sooke'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    			<div class="row">
    				<?php if (is_page(12)) {
    					$args = array(
    					'post_type' => 'restaurant',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'chilliwack'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4">
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    		</div>
    	   	</div>
    	   </div>
       	</section>
       </div>
    </div>
    
    <?php get_footer(); ?>
  2. typecconsulting
    Member
    Posted 1 year ago #

    Updated code without all the superfluous <div class="row">'s

    <?php
    /*
    Template Name: Restaurants
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content-background">
       <div class="container">
    	<div id="page-title">
    	    <h1>Restaurants</h1>
    	    <div class="arrow"></div>
    	</div>
       	<section id="content" role="main">
    	   <div class="row">
    		<div class="col-sm-3 col-md-3 left-sidebar">
    		   <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Restaurants Sidebar') ) : ?>
    		   <?php endif; ?>
    		</div>
    	   	<div class="col-sm-9 col-md-9 content-right">
    		<div id="restaurant-listings">
    		    <h2><?php the_title(); ?></h2>
    			<div class="row">
    <?php $count = 0; ?>
    				<?php if (is_page(8)) {
    				   $my_query = new WP_Query('post_type=restaurant&posts_per_page=-1');
    				   while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    
    				<?php endwhile;  wp_reset_query(); } ?>
    
    				<?php if (is_page(10)) {
    					$args = array(
    					'post_type' => 'restaurant',
    					'posts_per_page' => '-1',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'vancouver'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    
    				<?php if (is_page(19)) {
    					$args = array(
    					'post_type' => 'restaurant',
    					'posts_per_page' => '-1',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'toronto'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    
    				<?php if (is_page(16)) {
    					$args = array(
    					'post_type' => 'restaurant',
    					'posts_per_page' => '-1',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'victoria'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    
    				<?php if (is_page(14)) {
    					$args = array(
    					'post_type' => 'restaurant',
    					'posts_per_page' => '-1',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'kingston'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    
    				<?php if (is_page(165)) {
    					$args = array(
    					'post_type' => 'restaurant',
    					'posts_per_page' => '-1',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'sooke'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    
    				<?php if (is_page(12)) {
    					$args = array(
    					'post_type' => 'restaurant',
    					'posts_per_page' => '-1',
    				        'paged'=>$paged,
    				        'orderby'=>'title',
    				        'order'=>'ASC',
    					'tax_query' => array(
    				   array(
    					'taxonomy' => 'restaurant_category',
    					'field' => 'slug',
    					'terms' => 'chilliwack'
    					   )
    					)
    				);
    				$restaurants = new WP_Query( $args );
    				if( $restaurants->have_posts() ) {
    				   while( $restaurants->have_posts() ) {
      				   $restaurants->the_post();
    				?>
    				<div class="col-xs-4 col-sm-4 col-md-4" <?php if( $count%3 == 0 ) { echo ' style="clear: both;"'; }; $count++; ?>>
    				    <?php if ( has_post_thumbnail()) : ?>
       					<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" />
       					<?php the_post_thumbnail('', array('class' => 'img-responsive')); ?>
      					 </a>
     				    <?php endif; ?>
    				     <h3><a href="<?php the_permalink();?>"><?php the_title() ?></a></h3>
    				     <?php the_content() ?>
    				</div>
    				   <?php
    				   	 }
    				   }
    				   else {
    					echo '<div class="col-xs-12 col-sm-12 col-md-12">Coming Soon!</div>';
    				   }
    				   } ?>
    			</div>
    		</div>
    	   	</div>
    	   </div>
       	</section>
       </div>
    </div>
    
    <?php get_footer(); ?>

    Now the question is how to simply this AND add a count that outputs a new <div class="row"> after every third post. I found an slightly haphazard solution as you can see above, but I really would like to know how to actually output what I want, which is to close the original <div class="row"> after every third post with </div> and open another <div class="row"> again for the next three posts.

    I would hugely appreciate any help anyone could give! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic