How to Use Multiple Instances of the Same Conditional Tag on a Page?

Is there a way to use multiple instances of the same WP conditional tag on a page? I'm looking for a way to create a testimonials template that doesn't require multiple testimonial tags (testmonial1, testimonial2, testimonial3, etc.) wrapped in conditionals. But rather one tag that will be re-used as many times as is required, whether I've got 3 or 20 testimonials.

As a PHP beginner I have searched for this online, but I'm not sure how to define my search.

  • Rogier

    Hi Phil,

    Thanks for your reply. Essentially I would like to use WP conditional tags to create a testimonials page that consists of various divs that each consist of a quote plus the name of the person being cited. Right now this is what I have:

    <?php  if((get_post_meta($post->ID, "testimonial1_quote", true))) { ?>
    			<div class="testimonial">
    			<p><blockquote><?php echo get_post_meta($post->ID, 'testimonial1_quote', true); ?></blockquote></p>
    			<p><cite><?php echo get_post_meta($post->ID, 'testimonial1_cite', true); ?></cite></p>
    			</div>
    			<?php } ?>
    
    			<?php  if((get_post_meta($post->ID, "testimonial2_quote", true))) { ?>
    			<div class="testimonial">
    			<p><blockquote><?php echo get_post_meta($post->ID, 'testimonial2_quote', true); ?></blockquote></p>
    			<p><cite><?php echo get_post_meta($post->ID, 'testimonial2_cite', true); ?></cite></p>
    			</div>
    			<?php } ?>
    
    			<?php  if((get_post_meta($post->ID, "testimonial3_quote", true))) { ?>
    			<div class="testimonial">
    			<p><blockquote><?php echo get_post_meta($post->ID, 'testimonial3_quote', true); ?></blockquote></p>
    			<p><cite><?php echo get_post_meta($post->ID, 'testimonial3_cite', true); ?></cite></p>
    			</div>
    			<?php } ?>
    
    			<?php  if((get_post_meta($post->ID, "testimonial4_quote", true))) { ?>
    			<div class="testimonial">
    			<p><blockquote><?php echo get_post_meta($post->ID, 'testimonial4_quote', true); ?></blockquote></p>
    			<p><cite><?php echo get_post_meta($post->ID, 'testimonial4_cite', true); ?></cite></p>
    			</div>
    			<?php } ?>
    
    			<?php  if((get_post_meta($post->ID, "testimonial5_quote", true))) { ?>
    			<div class="testimonial">
    			<p><blockquote><?php echo get_post_meta($post->ID, 'testimonial5_quote', true); ?></blockquote></p>
    			<p><cite><?php echo get_post_meta($post->ID, 'testimonial5_cite', true); ?></cite></p>
    			</div>
    			<?php } ?>

    But it just seems very clunky. In this way I would always have to anticipate how many testimonials I will have, and if this falls short, I'd have to update the template to contain more custom fields.

    I would think there is a way in WP to do this which just requires one of these sets of code (as opposed to 5 in this example) and which could be cycled through to output as many testimonial divs as are required.

    I realize CustomPress may offer a solution here, but when activating the plugin it seemed to slow the site down. So if there is a way to do this without a plugin, it would be preferred.

  • Philip John

    Okay, first off my initial solution would be to use CustomPress so if that isn't working great for you, please open a new thread explaining the issues you're having with it so we can get it sorted for you :slight_smile:

    Secondly, you could do this with categories.

    Simply create a new category called "Testimonials" and add your testimonial posts to that category.

    Then you just go to http://example.com/category/testimonials to see all the testimonials.

    You could also add a new category-testimonials.php template file to your theme to customise that page.

    Hope that helps!
    Phil

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.