Skip to content
Advertisement

php $image = get_field( ‘image’ ) // if // else

I need an inquiry if there is no image that an alternative image is used.

<?php $angebot_17 = get_field( 'angebot_17' ); ?>
           <?php if ( $angebot_17 ) : ?>
               <?php $post = $angebot_17; ?>
               <?php $image = get_field( 'image' ); ?>
                       <?php if ( $image ) { ?>
                       <a href="<?php the_permalink(); ?>">
                           <img width="300" height="300" src="<?php the_field('image'); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" class="img-responsive product-img wp-post-image box_image" />
                           <?php $lager = get_field ('lager'); ?>
                           <?php if ( $lager) { ?>
                           <span class="goods_label" style="background-color: #00CC99;color: #ffffff;bottom:8px;border-color: #00CC99;">
                               <span>Versand aus Deutschland</span>
                           </span>
                           <?php } ?>
                       </a>
                       <?php } ?>
                       <?php else: ?>
                       <img width="300" height="300" src="/wp-content/uploads/2019/09/cropped-Logo_512x512-4.png" alt="<?php echo esc_attr( $image['alt'] ); ?>"  />
                       <span class="goods_label" style="background-color: red;color: #ffffff;bottom:8px;">
                               <span>Leider kein Bild vorhanden</span>
                           </span>
                       <?php wp_reset_postdata(); ?>

                       <?php endif; ?>

the crazy thing is that it works on four out of 18 images. I think I have an error in the “else query” somewhere. Because if I swap the defalut image with the actual product image, then it works .. but of course that is not meaningful;)

Advertisement

Answer

i think it works!!!

I wrote the whole code again “cleanly”!

Thanks for all the support!

<?php
    $angebot_1 = get_field( 'angebot_1' );
    if ($angebot_1) {
        $post = $angebot_1;
        $image = get_field( 'image' );

        if ($image) {
            echo '
                <img
                width="300"
                height="300"
                src="' . esc_url( $image['url'] ) .'"
                alt="' . esc_attr( $image['alt'] ) . '"
                class="img-responsive product-img wp-post-image box_image" /> ';

            $lager = get_field ('lager');
            if ($lager) {
                echo '<span
                        class="goods_label"
                        style="background-color: #00CC99;color: #ffffff;bottom:8px;border-color: #00CC99;">
                    <span>Versand aus Deutschland</span>
                </span> ';
                }

            }
            else {
                echo '<img
                        width="300"
                        height="300"
                        src="/wp-content/uploads/2019/09/cropped-Logo_512x512-4.png"
                        alt="'. esc_attr( $image['alt'] ).'"  /> ';
                echo '<span
                            class="goods_label" style="background-color: red;color: #ffffff;bottom:8px;">
                                <span>Leider kein Bild vorhanden</span>
                            </span> ';
            }
        wp_reset_postdata();
    }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement