简体   繁体   中英

How to add script and stylesheet to head only to one page on website

I want to add the following 2 lines only to the one page of the website

<script src="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>

And

<link href="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" media="screen" rel="stylesheet" type="text/css">

My <head> tag with all links is in a separate PHP file, which I include in all files. Is it possible to add this script and stylesheet only to one page where it's needed? On that separate PHP file, I have a function

$('.owl-carousel').owlCarousel({
   items: 1,
   nav: true,
   dots: false,
   loop: true
});

Just get your page url and if it is include your script and styles for that page

<?php
    $link = $_SERVER['PHP_SELF'];
    $link_array = explode('/',$link);
    $page = end($link_array);
    if($page == 'your_page_last_part') {
?>
<script src="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" media="screen" rel="stylesheet" type="text/css">
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM