1

I have a variable called $speakers that displays a list of speakers for an event when selected from a drop down list. The problem is when none of the speakers are selected for an event, the list of all speakers displays in that section instead of not displaying anything in that section like it should since nothing is selected. Here is the code block:

    $speakers = get_post_meta( $post->ID, 'min_webinar_speaker', false );

    $args = array(
      'posts_per_page'   => -1,
      'orderby'          => 'date',
      'order'            => 'DESC',
      'include'          => $speakers,
      'post_type'        => 'speaker',
      'post_status'      => 'publish',
      'suppress_filters' => true
    );
    $speakers = get_posts( $args );






    <?php for  ($i = 0; $i < count($speakers); $i++) {
                $speaker = $speakers[$i];
                if ( $i % 2 == 0 ) {
                    ?>
                    <div class="row">
                    <?php
                }
            ?>

                    <article class="subscriber col-sm-6">
                        <div class="row">
                            <div class="col-sm-6">
                                <?php echo get_the_post_thumbnail($speaker->ID, 'thumbnail'); ?>
                            </div>
                            <div class="col-sm-6 subscriber-desc">
                                <h5 class="moderator"><?php
                                    global $wp_query;
                                    $postid = $wp_query->post->ID;
                                    echo get_post_meta($speaker->ID, 'min_speaker_speaker_role', true);
                                    wp_reset_query();
                                    ?>    
                                </h5>
                                <h1><?php echo $speaker->post_title; ?></h1>
                                <?php echo wpautop( $speaker->post_content); ?>
                            </div>
                        </div>
                    </article>
                <?php //} ?>
                <?php 
                if ( $i % 2 == 1 || ($i+1) == count($speakers) ) {
                    ?> </div> <!-- test--><?php 
                }

            }?>

Any ideas on how to re work this code block to work properly would be greatly appreciated.

7
  • You dont want to show the whole block, so <div class="row"> to the last </div>, or you dont want to show something specific inside there?
    – The J
    Sep 1, 2016 at 22:08
  • If there are items in $speakers then this code will run. If you don't have speakers assigned, but whole list is showing up in the $speakers array, then the problem stems from whatever generated the $speakers array. Can you paste that code for us? Sep 1, 2016 at 22:56
  • Agreed with Jeremy. We need a bit more of your code for context to offer useful answers.
    – jdm2112
    Sep 2, 2016 at 3:14
  • ok give me a sec and I'll paste it in @JeremyRoss
    – MikeL5799
    Sep 2, 2016 at 14:56
  • @TheJ yeah I don't want the whole block to show
    – MikeL5799
    Sep 2, 2016 at 14:59

1 Answer 1

1

The issue is that when you don't have any speakers, you don't have anything to pass to your include parameter.

You could solve it by wrapping the code in a conditional so it only runs if there are speakers attached to that post.

$speakers = get_post_meta( $post->ID, 'min_webinar_speaker', false );

if ($speakers != '') { // <- you may need to do a more thorough check than this
    ... run the code...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.