WP Как добавить класс если отсутствует контент?
Есть необходимость в добавлении класса 'header_dark'
к тегу <body>
при условии что в контенте отсутствует тег <section>
Вот написал вот такой код, но он не работает.
function add_dark_class_to_head() {
if (is_singular()) {
$post_content = get_post_field('post_content', get_the_ID());
$main_section_pattern = '/<main\b[^>]*>(.*?)<section\b[^>]*>/s';
if (preg_match($main_section_pattern, $post_content)) {
add_filter('body_class', function ($classes) {
$classes[] = 'header_dark';
return $classes;
});
}
}
}
add_action('wp_head', 'add_dark_class_to_head');
Подскажите, в чем может быть проблема? Что я не так проверяю ? *Контент сохраняеться в стандартные таблицы WP
и задаеться через Classic Editor
header.php
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="initial-scale=1, width=device-width, height=device-height, viewport-fit=cover"><meta name="format-detection" content="telephone=no">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div class="wrapper">
<header>
</header>
<main id="main" class="main">
page.php
<?php get_header(); ?>
<div class="container">
<div id="content" class="col_content">
<div <?php post_class('post section py-md') ?> id="post-<?php the_ID(); ?>">
<div class="title">
<?php am_the_custom_title('h1'); ?>
</div>
<div class="entry">
<?php the_content(__('Read more', 'am')); ?>
<div class="clear clearfix"></div>
<?php wp_link_pages(array('before' => '<div class="page-link"><p><span>' . __('Pages:', 'am') . '</span>', 'after' => '</p></div>')); ?>
<?php edit_post_link(__('Edit', 'am'), '<br /><p>', '</p>'); ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
footer.php
</main>
<footer class="page-footer"></footer>
</div>
<?php wp_footer(); ?>
</body>
</html>