Ahmad Sharif
Ahmad Sharif

Reputation: 4435

Add multiple custom script in one js file for different wordpress page

I have written this script in a scripts.js file. And I want execute the first half for only Home page and second half for custom post type page. How can I do this in wordpress ?

 // This Chunk For Home Page  [Part one]
 jQuery("#front-page-slider").responsiveSlides({
              maxwidth: 390,
              "auto": true,
              "pager": false,
               "nav": false,
              "pause": true,
               speed: 800
          });



    // This Chunk is for For Only Custom Post Page [Part Two]
  $("a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'normal',theme:'pp_default',slideshow:3000, autoplay_slideshow: false});
  var width = $(window).width();

Upvotes: 1

Views: 254

Answers (2)

diggy
diggy

Reputation: 6828

As suggested in the comments, you can leverage the body class, e.g.:

if(jQuery('body.home').length){alert('home');}

Upvotes: 1

Enzo Zapata
Enzo Zapata

Reputation: 171

this is my idea:

in the js:

function PartOneForHome(){
//code bla bla bla
}
function PartTwoForCustomPosts(){
//code bla bla bla
}

in the header.php or footer.php:

<?php if(is_home()) { ?>
  <script> PartOneForHome(); </script>
<?php } else if is any custom post* { ?>
  <script> PartTwoForCustomPosts(); </script>
<?php } ?>

*I dont know how to detect if is a custom post, search for it :D

Upvotes: 0

Related Questions