在WordPress Sage的main.js中,我们可以运行特定于页面的JS:
var Sage = {
// All pages
'common': {
init: function() {
// JavaScript to be fired on all pages
},
finalize: function() {
// JavaScript to be fired on all pages, after page specific JS is fired
}
},
// Home page
'home': {
init: function() {
// JavaScript to be fired on the home page
},
finalize: function() {
// JavaScript to be fired on the home page, after the init JS
}
},
// About us page, note the change from about-us to about_us.
'about_us': {
init: function() {
// JavaScript to be fired on the about us page
}
}
};
但是如何在特定的单个或归档CPT页面上运行JS?
答案 0 :(得分:3)
查看您希望JS启动的页面/帖子/存档正文中的类名,并使用其中一个。例如,如果您具有名为“work”的自定义帖子类型,则工作档案主体元素将具有Options +FollowSymlinks
RewriteEngine on
rewriterule ^public/index.php/samepagename(.*)$ http://domain.com/site/samepagename$1 [r=301,nc]
类。您可以使用它来解雇您的特定JS:
post-type-archive-work
有关详细信息,请查看Paul Irish's post
答案 1 :(得分:0)
乔什·埃利斯(Josh Ellis)的回答是正确的,但有一些澄清。 类路由器具有以下方法:
/**
* Fire Router events
* @param {string} route DOM-based route derived from body classes (`<body class="...">`)
* @param {string} [event] Events on the route. By default, `init` and `finalize` events are called.
* @param {string} [arg] Any custom argument to be passed to the event.
*/
fire(route, event = 'init', arg) {
const fire = route !== '' && this.routes[route] && typeof this.routes[route][event] === 'function';
if (fire) {
this.routes[route][event](arg);
}
}
使用身体上的任何类。例如:Sage 9的'teams-template-default single single-teams'=>'single_teams'或'singleTeams'。