Sure thing, couple of pointers:
- No need for an empty
else
– just go straight to theendif
== true
is a redundant condition.if ( $var )
andif ( $var == true )
are the same. Note that$var === true
is different though (read up on comparison operators)- Use
site_url( $path )
instead ofget_site_url() . $path
– this will ensure the URL is properly concatenated (if$path
had no preceding slash you’d end up withexample.commypath
) - Sidenote: might want to re-think your mobile images technique – regardless of your CSS/JS, any device is going to request both images
And save yourself even more typing by assigning values within the condition. For example:
// This...
if ( get_post_meta( $id, $field ) )
echo get_post_meta( $id, $field );
// Less function overhead, neater
$value = get_post_meta( $id, $field );
if ( $value )
echo $value;
// Even neater
if ( $value = get_post_meta( $id, $field ) )
echo $value;
The result:
<?php
for ( $i = 1; $i <= 10; $i++ ) {
if ( $image = get_post_meta( $post->ID, "{$i}-carousel-image", true ) ) : ?>
<div class="item">
<?php if ( $title = get_post_meta( $post->ID, "{$i}-carousel-title", true ) ) : ?>
<div class="display-block">
<div class="text-block slider">
<h3 class="slider-title"><?php echo $title ?></h3>
<p><?php echo get_post_meta( $post->ID, "{$i}-carousel-text", true ) ?></p>
<?php if ( $text = get_post_meta( $post->ID, "{$i}-carousel-button-text", true ) ) : ?>
<a href="https://wordpress.stackexchange.com/questions/237132/<?php echo esc_url( site_url( get_post_meta( $post->ID,"{$i}-carousel-button-link", true ) ) ) ?>" class="button primary"><?php echo $text ?></a>
<?php endif ?>
</div>
</div>
<?php endif ?>
<img class="mobile-hide" src="<?php echo esc_url( $image ) ?>" alt="<?php echo esc_attr( get_post_meta( $post->ID, "{$i}-carousel-image-alt", true ) ) ?>" />
<img class="mobile-show" src="<?php echo esc_url( get_post_meta( $post->ID, "{$i}-carousel-image-mobile", true ) ) ?>" alt="<?php echo esc_attr( get_post_meta( $post->ID, "{$i}-carousel-image-alt", true ) ) ?>" />
</div>
<?php
endif;
}