简体   繁体   English

如何在特定的WordPress页面上删除style.css

[英]How to remove style.css on specific wordpress page

I have a wordpress with a child theme where in place where is wp_head(); 我有一个带有儿童主题的wordpress,其中wp_head()在哪里? style.css is added like: style.css添加如下:

<link rel='stylesheet' id='parent-style-css'  href='http://something' type='text/css' media='all' />

Id like to remove this style on specific page (lets say this page has ID=5). 我想在特定页面上删除此样式(假设此页面的ID为5)。 I've found how to do this in jQuery but it seems like a bad idea to remove styles client-side. 我已经找到了如何在jQuery中执行此操作,但是删除客户端样式似乎是一个坏主意。

How can I remove this style via php? 如何通过php删除此样式? possibly using https://codex.wordpress.org/Function_Reference/wp_dequeue_style but only on one specific page. 可能使用https://codex.wordpress.org/Function_Reference/wp_dequeue_style,但仅在一个特定页面上使用。

Put this code in your WP Theme Functions.php file. 将此代码放在WP Theme Functions.php文件中。 It should de-queue style files from specific pages: 它应该从特定页面出队样式文件:

 add_action('init','_remove_style');

 function _remove_style(){
    global $post;
    $pageID = array('20','30', '420');//Mention the page id where you do not wish to include that script

    if(in_array($post->ID, $pageID)) {
      wp_dequeue_style('style.css'); 
    }
 }

In theme functions.php you can use page id condition and put it inside it. 在主题functions.php中,您可以使用页面ID条件并将其放在其中。

global $post;
if($post->ID == '20'){
      // dequeue code here
    }

You can use is_page() function inside if condition to target only specific pages if条件仅定位到特定页面, if可以在内部使用is_page()函数

is_page() function takes any one of the following as parameters is_page()函数采用以下任意一项作为参数

  • ID (Ex: is_page(5) ) ID (例如: is_page(5)
  • Page Name (Ex: is_page('Contact Us') ) 页面名称 (例如: is_page('Contact Us')
  • Page Slug (Ex: is_page('contact-us') ) 页面弹头 (例如: is_page('contact-us')

Examples 例子

if(is_page(5)){
// wp_dequeue_style
}


if(is_page('Contact us')){
// wp_dequeue_style
}


if(is_page('contact-us')){
// wp_dequeue_style
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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