Home Forums Front-end Issues 1 Custom post type, 2 different meta-values, same loop? Reply To: 1 Custom post type, 2 different meta-values, same loop?

  • Hi Intrepidrealist

    Your code seems a bit overly complex for what you’re trying to achieve. Before I get into a solution based on your current approach I would urge you to check out the repeater field or flexible content field add-ons that Elliot has put together. These are paid for additions to ACF but worth every penny and would make this sort of thing an absolute breeze compared to using custom post types for something like slides on a slider. If you are create a site for a client they will thank you!

    If you can’t or won’t go for these options then your code could be simplified quite a bit. Please see the stripped down example below. The biggest issue I could see is that you mention you have a radio button field but in your code you are not comparing your field against a proper value in your if statements. Please see the inline comments for more info. It’s not production code but hopefully it will give you a nod in the right direction

    //Instead of the meta query in your arguments just call for all slide posts - you can add ordering here if you want though
    $args = array( 'post_type' => 'slide', 'posts_per_page' => -1, ); 
    $loop = new WP_Query( $args ); 
    /*Just as a bit of good practice, if you call your if have_posts conditional before all slider content in the event 
    that there are no slides the whole slider will not show as opposed to showing an empty slider with no content*/
    if($loop->have_posts()) :  ?>
    <div class="flexslider">
    	  <ul class="slides">
    	  <?php while ($loop->have_posts()) : $loop->the_post(); // We then start looping through the slides ?>
    			<?php if ( get_field('slide_image') !=false) { 
    			/* Here's where you problems start I think.  
    			Your radio field should have at least a couple of values e.g. you could 
    			have image_slide and video_slide as your two values on your radio field
    			Your if statement above would then look like this:
    			if ( get_field('slide_image') == 'image_slide') {
    				<!-- Your slide content goes here -->
    			Your version of the code below looked a bit broken - this you would have got a syntax error as your else if statement was malformed
    			The version below copies the example I gave you above but checks to see if the slide is a video slide
    			} elseif ( get_field('static_slide_image') == false) { ?>
    				<!-- Your slide content goes here -->
    			<?php  } ?>
    			<?php //close off loop
    			endwhile; ?>