I am trying to load some stylesheets only on certain “pages” that contain a specific class on a WordPress website.
I want to load stylesheets
add_action( 'wp_enqueue_scripts', function() {
wp_dequeue_style('my-child');
wp_enqueue_style('style1_css', '/wp-content/plugins/myplugin/assets/style1.css' , 'all' );
wp_enqueue_style('style2_css', '/wp-content/plugins/myplugin/assets/style1.css' , 'all' );
}, 1 );
I have tried all sorts of suggestions like the following that is not working.
function check_class_in_content($class) {
if (is_singular()) {
global $post;
if (strpos($post->post_content, $class) !== false) {
return true;
}
}
return false;
}
if (check_class_in_content('page-id-2022')) {
echo 'Class exists in the content of this page.';
} else {
echo 'Class does not exist in the content of this page.';
}
What am I doing wrong and how can I get it to work?
Thanks