我使用了这两个插件,名为“珠穆朗玛峰标签”和“简单推荐信”仅在我的一个页面与id="15280",但他们的CSS和JS脚本加载到所有的页面。因为限制了脚本,我尝试了两种方法,但是,没有起作用。我不知道我做错了什么。有没有办法解决这个问题?
第一种方法,我在functions.php文件的末尾添加了下面的代码。
function conditionally_load_plugin_js_css(){
if(! is_page( array(15280) ) ){ # Load CSS and JS only on Pages with ID 4 and 12
wp_dequeue_script('easy-testimonials'); # Restrict scripts.
wp_dequeue_style('easy-testimonials'); # Restrict css.
wp_dequeue_script('everest-tab-lite'); # Restrict scripts.
wp_dequeue_style('everest-tab-lite'); # Restrict css.
}
}
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css' );第二种方法,我在functions.php文件的末尾添加了下面的代码。
function remove_wpetl_extras() {
remove_action('wp_print_scripts', 'wpetl_enqueue_scripts');
remove_action('wp_print_styles', 'wpetl_enqueue_styles');
}
if( ! is_page('15280') ) {
add_action('wp_head', 'remove_wpetl_extras');
}发布于 2019-11-21 09:28:32
我想你只需要检查一下订单优先级
优先级(int) (可选),用于指定与特定操作相关的函数的执行顺序。较低的数字对应于更早的执行,具有相同优先级的函数按照添加到操作的顺序执行。默认值: 10 文档
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css', 100);将优先级更改为100或更高的999,因为大多数插件使用99之类的高值或更高的值,您需要重写它。
还请注意,如果您想要完全删除一个样式或脚本,您需要dequeue您的脚本然后deregister它。
编辑:
我明白问题所在了。我已经测试了代码,它可以工作,但您必须编写样式/脚本的确切名称。
在您的示例中,两个插件都添加了一些样式表和脚本,Easy Testimonials添加了6个样式表和2个脚本:
easy_testimonial_style
single-testimonial-block
random-testimonial-block
testimonials-list-block
testimonials-cycle-block
testimonials-grid-block
easy-testimonials-reveal
gp_cycle2 (jquery.cycle2)Everest Tab Lite又添加了3个样式表和1个脚本:
et-frontend-style
et_fontawesome_style
et-animate-style
et-frontend-script请注意,WordPress将在样式表名称的末尾自动添加
-css。即et-frontend-style将显示为et-frontend-style-css
所以你的职能是:
function conditionally_load_plugin_js_css() {
// Load CSS and JS only on Pages with 15280
if(! is_page( array(15280) ) ) {
// Easy testimonial
wp_dequeue_style('easy_testimonial_style');
wp_dequeue_style('single-testimonial-block');
wp_dequeue_style('random-testimonial-block');
wp_dequeue_style('testimonials-list-block');
wp_dequeue_style('testimonials-cycle-block');
wp_dequeue_style('testimonials-grid-block');
// scripts
wp_deregister_script('gp_cycle2');
wp_deregister_script('easy-testimonials-reveal');
// Everest Tab Lite
wp_dequeue_style('et-frontend-style');
// Keep these if you like fontawesome and animate.css
wp_dequeue_style('et_fontawesome_style');
wp_dequeue_style('et-animate-style');
// script
wp_deregister_script('et-frontend-script');
}
}
// Everest Tab Lite has priority more than 9999 so i changed the priority to 99999
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css', 99999);https://stackoverflow.com/questions/58970520
复制相似问题