I am using Advanced Custom Fields Pro for adding elements to a web page.

There is a section about embedding a YouTube video. You simply add the video URL, and it shows up on the page. I have added the video in the right sidebar, along with a H2 headline (Media) for the video section.

Right now if I do not add a URL in the YouTube video section, then the page shows the H2 (Media) and a blank spot beneath it where a video should be embedded.

I am wanting the entire “Media” section to not show up at all when no YouTube URL is entered. So, if a URL is entered, then the “Media” section will show on the page.

Here is the HTML for the “Media” section on the right sidebar.

<!-- Media/YouTube Video Section -->
<div class="resourcespdf">
<h2 class="customresourcespdf">Media</h2>
</div>


<div class="embed-containerone">                    
<?php
if(get_field('youtube_video_1')){
the_field('youtube_video_1');
}
?>                  
</div>

1 Answer
1

All you have to do is to use if statement correctly in your code:

<?php if (get_field('youtube_video_1')) : ?>
<!-- Media/YouTube Video Section -->
<div class="resourcespdf">
<h2 class="customresourcespdf">Media</h2>
</div>


<div class="embed-containerone">                    
<?php the_field('youtube_video_1'); ?>                  
</div>
<?php endif; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *