2

I have a file called smart screen-front.php. Inside this file I am trying to output some php variables inside some javascript:

<?php
header("Content-type: text/javascript"); // This bit must come first!
?>
<?php $ssimage1 = get_field('smartscreen_slider_image_1'); ?>
<?php $ssimage2 = get_field('smartscreen_slider_image_2'); ?>
<?php $ssimage3 = get_field('smartscreen_slider_image_3'); ?>
<?php $ssimage4 = get_field('smartscreen_slider_image_4'); ?>
<?php $ssimage5 = get_field('smartscreen_slider_image_5'); ?>
<?php $ssimage6 = get_field('smartscreen_slider_image_6'); ?>
<?php $ssimage7 = get_field('smartscreen_slider_image_7'); ?>
<script type="text/javascript">
 jQuery(document).ready(function () {

            jQuery.supersized({

                // Functionality
                slide_interval          :   10000,      // Length between transitions
                transition              :   1,          // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
                transition_speed        :   1000,
                keyboard_nav            :   1,

                // Speed of transition

                // Components
                slide_links             :   'blank',    // Individual links for each slide (Options: false, 'num', 'name', 'blank')
                slides                  :   [           // Slideshow Images
                    {image : <?php echo $ssimage1; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''} ,
                    {image : <?php echo $ssimage2; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>', thumb : '', url : ''} ,
                    {image : <?php echo $ssimage3; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>', thumb : '', url : ''} ,
                    {image : <?php echo $ssimage4; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''} ,
                    {image : <?php echo $ssimage5; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''},
                    {image : <?php echo $ssimage6; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''} ,
                    {image : <?php echo $ssimage7; ?>, title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''}

                    ]
            });
        }

);
</script> 

This is not outputting the php variables. What am I doing wrong.

2
  • What happens if you just try to output those variables into a <div/>? Are they being populated at all? Commented Mar 2, 2015 at 3:57
  • is your get_field() function has worked properly ? Commented Mar 2, 2015 at 4:01

1 Answer 1

1

It looks like you may need to wrap the <?php echo ...; ?> statements in the javascript in quotes:

'<?php echo ...; ?>'

or

"<?php echo ...; ?>"

As it stands now, assuming each $ssimageX contains a string looking something like "X.jpg", the javascript ends up looking like

{image : X.jpg, title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''}

Which would result in a ReferenceError unless X is defined, and could be a SyntaxError if the url has a / in it.

My guess is you want it to look more like

{image : 'X.jpg', title : '<div class="slider-caption"><h2></h2><p></p></div>',  thumb : '', url : ''}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.