add_filter the_content в functions.php не работает
Я хочу добавить add_filter для the_content в functions.php моей темы. Я добавил код, который просто выводит echo, но похоже, что мой фильтр не применяется.
function add_mod_hatom_data($content) {
// $t = get_the_modified_time('F jS, Y');
//$author = get_the_author();
// $title = get_the_title();
//if(is_single()) {
echo 'perrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
// $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> был изменен: <span class="updated"> '.
//$t.'</span> автором <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
// }
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');
Я пробовал вызывать его так:
add_filter('the_content', 'add_mod_hatom_data', 99);
Или изменить позицию, чтобы он был вверху functions.php, но без успеха.
Нужно ли где-то включать add_filter или он переопределен другой функцией? ПРИМЕЧАНИЕ: В моем шаблоне single post есть:
<?php get_header(); ?>
<div id="content" class="clearfix row">
<div id="main" class="col-sm-8 clearfix" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<header>
<div class="page-header clip_content single-post">
<div class="page-header">
<h1 class="single-title-wg" itemprop="headline">
<?php $category = get_the_category();
if ($category[0]) {
echo '<b>'.$category[0]->cat_name.' '.$post->ID.'</b>';
}
?>
</h1>
<h4 class="single-title" itemprop="headline">
<p class="lead">
<?php the_title(); ?>
</p>
</h4>
</div>
<div class="item_footer">
</div>
<?php $category = get_the_category();
if ($category[0]) {?>
<p class="authorParagraph">
<?php echo '<a href="' . get_category_link($category[0]->term_id) . '" class="clearfix">' . $category[0]->cat_name . '</a></p>';
}
?>
</div>
</div>
</header> <!-- конец заголовка статьи -->
<footer>
<!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Теги","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
</footer> <!-- конец подвала статьи -->
</article> <!-- конец статьи -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Не найдено", "wpbootstrap"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Извините, но запрашиваемый ресурс не был найден на этом сайте.", "wpbootstrap"); ?></p>
</section>
<footer>
</footer>
</article>
<?php endif; ?>
</div> <!-- конец #main -->
</div> <!-- конец #content -->
<?php get_footer(); ?>

Это происходит потому, что переменная '$content' пуста. Способ использования этого фильтра правильный. Вместо 'echo' поместите свои значения в переменную '$content', и тогда 'return $content' фактически выведет их на страницу. Попробуйте так:
function add_mod_hatom_data($content) {
// $t = get_the_modified_time('F jS, Y');
//$author = get_the_author();
// $title = get_the_title();
//if(is_single()) {
$content = 'perrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
// $content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> было последний раз изменено: <span class="updated"> '.
//$t.'</span> автором <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
// }
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');

Это тоже не работает. Я закомментировал код в этой функции и добавил echo, чтобы доказать, что функция не вызывается. Я также протестировал ваш код — 'perrrrr...' не отображается, даже если я поместил его в переменную $content. Я подозреваю, что это связано с тем, что в шаблоне одиночной записи у меня используется the_post(), а не the_content() или что-то подобное?

Можете добавить код вашего шаблона одиночной записи, чтобы мы могли его увидеть? (Весь код)

Эта часть шаблона отдельной записи называется циклом:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Внутри цикла необходимо вывести содержимое записи:
<?php the_content(); ?>
Замените ваш шаблон отдельной записи на этот код и проверьте, работает ли он теперь:
<?php get_header(); ?>
<div id="content" class="clearfix row">
<div id="main" class="col-sm-8 clearfix" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article" itemscope itemtype="http://schema.org/BlogPosting">
<header>
<div class="page-header clip_content single-post">
<div class="page-header">
<h1 class="single-title-wg" itemprop="headline">
<?php $category = get_the_category();
if ($category[0]) {
echo '<b>'.$category[0]->cat_name.' '.$post->ID.'</b>';
}
?>
</h1>
<h4 class="single-title" itemprop="headline">
<p class="lead"> <?php the_title(); ?></p></h4>
<p><?php the_content(); ?></p>
</div>
<div class="item_footer">
</div>
<?php $category = get_the_category();
if ($category[0]) {?>
<p class="authorParagraph">
<?php echo '<a href="' . get_category_link($category[0]->term_id) . '" class="clearfix">' . $category[0]->cat_name . '</a></p>';
}
?>
</div>
</header> <!-- конец заголовка статьи -->
<footer>
<!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Теги","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
</footer> <!-- конец подвала статьи -->
</article> <!-- конец статьи -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Не найдено", "wpbootstrap"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Извините, запрашиваемый ресурс не был найден на этом сайте.", "wpbootstrap"); ?></p>
</section>
<footer>
</footer>
</article>
<?php endif; ?>
</div> <!-- конец #main -->
</div> <!-- конец #content -->
<?php get_footer(); ?>
