First time poster. PHP idiot.
I need to (re)write code that will display a div if a field is entered but to hide it if it's empty. The field is populated using a custom field in the backend of Wordpress.
I wrote this code many years ago and it used to work but no longer does. My skill level is 0. This is it:
<?php
$video = get_post_meta($post->ID, 'video_file_name', true);
if ($video == '')
{ ?>
<div class="hide">
<!-- hide video -->
</div>
<?php } else { ?>
<div class="show">
This a javascript block that shows a video player.
<script src="this_code_is_hidden.js"> </script>
<div id="mediaspace">text will be replaced</div>
<script type="text/javascript">
jwplayer('mediaspace').setup({
'file': 'http://video.xxxxx.com.au/<?php the_field('video_file_name'); ?>',
"advertising":
{
'client': "vast",
'tag': google ad invocation code
},
'title': '<?php the_title(); ?>',
'ga': {
'idstring': '<?php the_title(); ?>',
'label': '<?php the_title(); ?>'
},
'image': '<?php the_field('poster_frame'); ?>',
"sharing": {
"link": "http://www.xxxxx.com.au"
},
'controlbar': 'bottom',
'stretching': 'fill',
'width': '100%',
'aspectratio': '16:9',
'height': '332'
});
</script>
</div>
<?php } ?>
I've hidden some bits for security.
The theory is when ('video_file_name') is populated it will show the javascript block which will display a video.
I use CSS classes to either hide or show.
If anyone can shed some light I would be most appreciative. I've spent hours searching and trying but not clever enough to make it work.
I don't know if you can 'echo' a whole code block.
Thank you.
if ($video == '')
toif( empty( $video ) )
.<?php $video = get_post_meta($post->id, 'video_file_name', true); if ( empty( $video ) ) { ?> <div style="display:none;"> <!-- hide video --> </div> <?php }
ATM moment when the text field 'video_file_name' is entered, it's hiding the div - the opposite I was hoping for!