Support

Account

Home Forums Front-end Issues Display images from url in Text field

Solved

Display images from url in Text field

  • I am building a Carousel that displays images that are loaded on a separate folder on the server and not into the WP Media gallery.

    The images are store in ACF in a Text filed, something like https://www.domain.com/assetts/images/image-1.jpg

    How can I display the images in the carousel, using my code? What I get at the moment s nothing, just a Carousel without images

    <?php
        		//code to display the banners
        		//check that the page has banners
    			if( have_rows('images') ): 
            //count the number of banners
            $count = count(get_field('images'));?>
            <div id="banner-carousel" class="carousel slide" data-ride="carousel">
            <?php 
            //add the indicators only if there is more than one banner
            if ($count > 1) {?>
                <ol>
               <?php $i=0; ?>
                <?php while ($i < $count) { ?>
                    <li data-target="#banner-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) {echo'active';} ?>"></li>
                <?php $i++; } ?>
                </ol>
            <?php } ?>
            <?php //loop through all the banners ?>
            <?php $i=0; ?>
                <div class="carousel-inner">
            <?php while( have_rows('images') ): the_row(); 
            
                //get the banner variables. 
                $images = get_sub_field('image');            
            ?>
    			<div style="background: url('<?php echo $image['url']; ?>') no-repeat center center; background-size: cover;" class="carousel-item <?php if ($i == 0) {echo'active';} ?>">
                    <div class="container">
                        <div class="row carousel-mieten">
                        </div>
                    </div>
                </div>
                
                <?php $i++; ?>
            <?php endwhile; ?>
                </div>
    			
               <?php if ($count > 1) {?>
                <a href="#banner-carousel">
                    <span class="carousel-control-prev-icon" aria-hidden="true"></span>   
                </a>
                <a href="#banner-carousel">
                    <span class="carousel-control-next-icon" aria-hidden="true"></span>    
                </a>
                <?php } ?>
                </div>
    <?php endif; 
    ?>
  • Not sure if this will cause your issue, but the first think to change is that you need to get the repeater before you use have_rows().

    
    $images = get_field('images');
    if (!empty($images)) {
      $count = count($images);
    }
    if (have_row('images')) {
       // your code continues
    

    The next issue is that you are attempting to use a text field like an image field. A text field only returns the value entered into it

    
    $image = get_sub_field('image');
    ..... background: url('<?php echo $image; ?>') ...
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.