• 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. ]

    <?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)
  • 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

    (@lisaharr)

    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

    (@lisaharr)

    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.