Title: Create a conditional logic inside an array
Last modified: August 24, 2016

---

# Create a conditional logic inside an array

 *  [lisaharr](https://wordpress.org/support/users/lisaharr/)
 * (@lisaharr)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/create-a-conditional-logic-inside-an-array/)
 * I am basically looking to tidy up my code. I have a custom post type with categories.
   I then have particular categories displaying on certain pages. I don’t want to
   repeat the same thing each time and only changing the category type. I want to
   create an if statement inside of the array but have no idea how to go about it.
   Open to any suggetions
 * Here is my code:
 * _[ Moderator note: Code fixed, please wrap code in [backticks or use the code button](http://codex.wordpress.org/Forum_Welcome#Posting_Code).]_
 *     ```
       <?php if( is_page('24655') ) : ?>
       <section>
       	<ul id="thumbnailGrid">
       		<?php
               $args=array(
                 'testimonial-type' => '200-hours-testimonial', //this is what I change
                 'post_type' => 'testimonials',
                 'post_status' => 'publish',
                 'posts_per_page' => 10,
               );
               $my_query = null;
               $my_query = new WP_Query($args);
               if( $my_query->have_posts() ) {
                 while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
                           <li class="ft_promo col-md-3 col-sm-6">
                           <?php global $post; ?>
                               <?php
                               $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
                               ?>
                                   <img src="<?php echo $src[0]; ?>" />
                                   <h3><?php the_title(); ?></h3>
                                   <p><?php the_content(); ?></p>
   
                       <?php
                 endwhile;
               }
               wp_reset_query();
               ?>
               <div class="clearfix"></div>
   
       </section>
   
       <?php elseif (is_page('24732') || is_page('24794')): ?>
       <section>
       ```
   
 * repeat as above etc. etc. etc.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [John Parris](https://wordpress.org/support/users/mindctrl/)
 * (@mindctrl)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/create-a-conditional-logic-inside-an-array/#post-6173844)
 * Hi,
 * If you have specific pages (as in the Page post type) mapped to specific testimonial
   types, you could build an array of that relationship and use a getter type function
   to return the testimonial type based on the page ID.
 * Something like:
 *     ```
       function my_testimonial_map( $id ) {
         $relationship = array(
           '24655' => '200-hours-testimonial',
           '24732' => 'another-one-here'
         );
         if ( in_array( $id, $relationship ) ) {
           return $relationship[$id];
         } else {
           return false;
         }
       }
       ```
   
 * Call it like `$testimonial_type = my_testimonial_map( get_the_ID() );` and pass
   it into the $args array.
 * Didn’t test that code, but you get the idea.
 *  Thread Starter [lisaharr](https://wordpress.org/support/users/lisaharr/)
 * (@lisaharr)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/create-a-conditional-logic-inside-an-array/#post-6173865)
 * Thank-you for your reply. So it is pulling in all categories. Have I done it 
   correctly?
 * **Function:**
    function my_testimonial_map( $id ) { $relationship = array( ‘24655’
   => ‘200-hours-testimonial’, ‘24732’ => ‘300-hours-testimonial’, ‘24794’ => ‘300-
   hours-testimonial’, ‘about’ => ‘about-page’ ); if ( in_array( $id, $relationship)){
   return $relationship[$id]; } else { return false; } }
 * **Template:**
    <section> <ul id=”thumbnailGrid”> <?php $args=array( $testimonial_type
   = my_testimonial_map( get_the_ID() ) ); $my_query = null; $my_query = new WP_Query(
   $args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query-
   >the_post(); ?>
 *  <li class=”ft_promo col-md-3 col-sm-6″>
    <?php global $post; ?> <?php $src =
   wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000),
   false, ” ); ?> <img src=”<?php echo $src[0]; ?>” /> <h3><?php the_title(); ?>
   </h3> <p><?php the_content(); ?></p>
 *  <?php
    endwhile; } wp_reset_query(); // Restore global post data stomped by 
   the_post(). ?> <div class=”clearfix”></div>
 * </section>
 *  Thread Starter [lisaharr](https://wordpress.org/support/users/lisaharr/)
 * (@lisaharr)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/create-a-conditional-logic-inside-an-array/#post-6173866)
 * It is actually bringing in my posts not the custom posts

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Create a conditional logic inside an array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [lisaharr](https://wordpress.org/support/users/lisaharr/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/create-a-conditional-logic-inside-an-array/#post-6173866)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
