我有一个带子主题的wordpress,其中的位置是wp_head();添加style.css
类似于:
<link rel='stylesheet' id='parent-style-css' href='http://something' type='text/css' media='all' />
我想删除特定页面上的这个样式(假设这个页面的Id为5)。我已经在jQuery中找到了如何做到这一点,但删除客户端样式似乎是个坏主意。
如何通过php删除此样式?可能使用https://codex.wordpress.org/Function_Reference/wp_dequeue_style但仅在一个特定页面上。
将此代码放入您的WP Theme Functions.php文件中。它应该从特定页面取消队列样式的文件:
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');
}
}
在主题functions.php中,您可以使用页面id条件并将其放入其中。
global $post;
if($post->ID == '20'){
// dequeue code here
}
您可以在if
条件内使用is_page()
函数仅针对特定页面
is_page()
函数采用以下任意一项作为参数
-
ID
(例如:
is_page(5)
) -
页面名称
(例如:
is_page('Contact Us')
) -
翻页
(例如:
is_page('contact-us')
)
示例
if(is_page(5)){
// wp_dequeue_style
}
if(is_page('Contact us')){
// wp_dequeue_style
}
if(is_page('contact-us')){
// wp_dequeue_style
}