Support

Account

Home Forums General Issues Layout breaks if field not entered

Solved

Layout breaks if field not entered

  • Hi, I’m new to ACF and have been having an issue with setting up a news feed on a new website.

    I’ve basically got fields for – News Title, News/Event Date, Location, Other Information, Download link, and News Thumbnail image.

    My issue is that if the ‘Download link’ doesn’t have anything uploaded to it then it breaks the site layout as it doesn’t seem to restart the loop for the next item.

    It’s probably something pretty straightforward but my code is below (this is part of a custom page template and is not the full code for the page):

    <?php 
    $args = array( 
    	'post_type'			=> 'news',
    	'posts_per_page'	=> 3,
    	'meta_key'			=> 'newsevent_date',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'DESC'
    );
    $the_query = new WP_Query( $args );
    ?>
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<div class="news-item">
    			<img  class="news-image" src="<?php the_field('news_thumbnailicon'); ?>" alt="" />
    			<div class="news-text">
    			<h2 class="news-title"><?php the_title() ;?></h2>
    			<p class="news-date"><?php the_field('newsevent_date');?></p><br>
    			<p class="location"><?php the_field('location');?></p>
    			<p class="other-info"><?php the_field('other_information');?></p>
    			<?php if( get_field('linkdownload') ): ?>
    
    				<a class="news-download" target="_blank" href="<?php the_field('linkdownload'); ?>" >Download File</a>
    
    			<?php endif; ?>
    			</div>
    		</div>											
    <br>
    <?php endwhile; else: ?> <p>Sorry, there are no posts to display</p> <?php endif; ?>
    <?php wp_reset_query(); ?>

    many thanks
    Kieran

  • try changing that if statement to

    
    <?php if( get_field('linkdownload') ) { ?>
    
    				<a class="news-download" target="_blank" href="<?php the_field('linkdownload'); ?>" >Download File</a>
    
    			<?php } ?>
    
  • Thank you very much, John. Much appreciated. That works perfectly.

    On another quick note (if you don’t mind). I’m also trying to display the filename instead of the ‘Download Report’ text, however, the method below is showing the full URL path. Any ideas?

    <?php if( get_field('linkdownload') ) { ?>
    
    				<a class="news-download" target="_blank" href="<?php the_field('linkdownload'); ?>" >Download File: <?php the_field('linkdownload'); ?></a><br>
    
    			<?php } ?>
  • Hi again John

    I’ve figured the filename issue out. Many thanks for your initial answer as it’s worked perfectly.

    Thanks again
    Kieran

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Layout breaks if field not entered’ is closed to new replies.