Custom shortcode – random.php

<?php

/**
 * Parses the passed attributes.
 */
$attributes = (array) cptui_shortcode_atts( $attributes );

$custom_query = new WP_Query( cptui_filter_query( $attributes ) );
?>
<div class="cptui-shortcode-random cptui-random-<?php echo esc_attr( $attributes['cptui_shortcode_id'] ); ?>">
    <?php if ( $attributes['title'] ) { ?>
        <h2><?php echo esc_html( $attributes['title'] ); ?></h2>
    <?php } ?>
    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
        <figure class="random-image random-image-<?php the_ID(); ?>">
            <a href="<?php the_permalink(); ?>">
        <?php
            the_post_thumbnail(
                'medium',
                array(
                    'alt' => __( 'Featured image for displayed random post.', 'cptui-extended-random' ),
                )
            );
        ?>
            </a>
        <?php if ( 'true' === $attributes['show_caption'] ) { ?>
            <figcaption><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></figcaption>
        <?php } ?>
        </figure>
    <?php endwhile; ?>
</div>
<?php
wp_reset_postdata();