在一个js文件中为不同的wordpress页面添加多个自定义脚本

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

本文关键字:wordpress 脚本 自定义 添加 一个 js 文件      更新时间:2023-09-26

我已经在scripts.js文件中编写了这个脚本。我想只执行主页的前半部分和自定义帖子类型页面的后半部分。我怎样才能在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();

正如评论中所建议的,你可以利用 body 类,例如:

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

的想法:

在 js 中:

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

在页眉.php或页脚中.php:

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

*我不知道如何检测是否是自定义帖子,请搜索:D