Best way to add script dynamically

I am using a javascript/jquery plugin called Royalslider.

The slider is initalized with a script (example below). Essentially, I need two versions of it. One if the post has a “gallery” (made in Advanced Custom Fields), and the other if it just has a standard featured image. I don’t need or want a script if it doesn’t have either.

I am leaning towards adding to my functions.php if statements with wp_enqueue_script to add the script.

The other option would be to echo the script out in my template file.

I’m just stuck with which would be the best way to do this.

<script>
jQuery(document).ready(function($) {
  $('#post-default-slider').royalSlider({
    fullscreen: {
      enabled: true,
      nativeFS: true
    },
    controlNavigation: 'thumbnails',
    autoScaleSlider: true, 
    autoScaleSliderWidth: 960,     
    autoScaleSliderHeight: 640,
    loop: true,
    imageScaleMode: 'fit-if-smaller',
    navigateByClick: true,
    numImagesToPreload:2,
    arrowsNav:true,
    arrowsNavAutoHide: true,
    arrowsNavHideOnTouch: true,
    keyboardNavEnabled: true,
    fadeinLoadedSlide: true,
    globalCaption: false,
    globalCaptionInside: false,
    thumbs: {
      appendSpan: true,
      firstMargin: true,
      paddingBottom: 4
    }
  });
});
</script>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.