0

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.

7
  • Try changing this if ($video == '') to if( empty( $video ) ). May 5 at 15:10
  • In what way is it not working?
    – vancoder
    May 5 at 17:46
  • Hi Tony, I'll give it a shot. May 8 at 1:38
  • Hi Vancoder, it's not hiding the div when the text field is empty. May 8 at 1:40
  • Hi Tony @tony, I've tried your suggestion and unfortunately it hasn't worked. Have I set up my variable incorrectly. <?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! May 8 at 2:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.