0

I have a CPT that displays any attached images via a slider.

However, when there are no attached images the slider still appears.

Is there a way to add a conditional IF statement that will hide the slider or at the very least return some inline css that will "display: none" on the container, to the code below? This is part of a cpt-single.php page.

<ul id="slider" class="exhibit-slide">
  <?php $args = array( 
      'post_type'      => 'attachment', 
      'orderby'        => 'menu_order', 
      'order'          => 'ASC', 
      'post_mime_type' => 'image',
      'post_status'    => null, 
      'numberposts'    => null, 
      'post_parent'    => $post->ID
    );

    $attachments = get_posts( $args );

    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            $alt          = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true );
            $image_title  = $attachment->post_title;
            $caption      = $attachment->post_excerpt;
            $description  = $attachment->post_content;
  ?>    

    <li>
        <div class="exhibit">
            <img src="<?php echo wp_get_attachment_url($attachment->ID); ?>" alt="<?php echo $alt; ?>">
        </div>
    </li>

  <?php } } ?>
</ul>
0

You need to add the query before the actualy displaying of the div, then you can run your conditional. According to get_posts() it returns an array, so I'm assuming it returns an empty array if posts aren't found, thus we can check is it's not ! empty()

<?php $args = array( 
        'post_type' => 'attachment', 
        'orderby' => 'menu_order', 
        'order' => 'ASC', 
        'post_mime_type' => 'image',
        'post_status' => null, 
        'numberposts' => null, 
        'post_parent' => $post->ID 
    );

    $attachments = get_posts( $args );
    if( ! empty( $attachments ) ) :
?>
    <ul id="slider" class="exhibit-slide">

        <?php foreach ( $attachments as $attachment ) {
            $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
            $image_title = $attachment->post_title;
            $caption = $attachment->post_excerpt;
            $description = $attachment->post_content;
        ?>  

            <li>
                <div class="exhibit">
                    <img src="<?php echo wp_get_attachment_url($attachment->ID); ?>" alt="<?php echo $alt; ?>">
                </div>
            </li>

        <?php } ?>
    </ul>
<?php endif; ?>

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.