добавить фильтр содержимого в functions.php не работает
Я хочу добавить фильтр для the_content в functions.php моей темы. Я добавил код, который просто отображает эхо, но он показывает, что мой фильтр не применяется.
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> was last modified: <span class="updated"> '.
//$t.'</span> by <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 или он переопределяется какой-то другой функцией? ПРИМЕЧАНИЕ: В моем шаблоне одного сообщения у меня есть:
<?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> <!-- end article header -->
<footer>
<!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
</footer> <!-- end article footer -->
</article> <!-- end article -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
</section>
<footer>
</footer>
</article>
<?php endif; ?>
</div> <!-- end #main -->
</div> <!-- end #content -->
<?php get_footer(); ?>
3 answers
Эта часть шаблона одиночной записи называется циклом:
<?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>
</div>
</header> <!-- end article header -->
<footer>
<!-- <?php the_tags('<p class="tags"><span class="tags-title">' . __("Tags","wpbootstrap") . ':</span> ', ' ', '</p>'); ?> -->
</footer> <!-- end article footer -->
</article> <!-- end article -->
<?php endwhile; ?>
<?php else : ?>
<article id="post-not-found">
<header>
<h1><?php _e("Not Found", "wpbootstrap"); ?></h1>
</header>
<section class="post_content">
<p><?php _e("Sorry, but the requested resource was not found on this site.", "wpbootstrap"); ?></p>
</section>
<footer>
</footer>
</article>
<?php endif; ?>
</div> <!-- end #main -->
</div> <!-- end #content -->
<?php get_footer(); ?>
Это происходит потому, что переменная '$content' пуста. Способ, которым вы используете этот фильтр, является правильным. Вместо "эхо" поместите свои значения в "переменную $content", а затем "возврат $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> was last modified: <span class="updated"> '.
//$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
// }
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');
Используйте это (фильтр_контента имеет только один параметр, и вы должны использовать 1)
add_filter('the_content', 'add_mod_hatom_data', 1);