admin管理员组文章数量:1287856
trying to filter the output of term_description to include a class
function add_class_to_term_description() { ?>
<?php echo '<div class="cell">' . term_description() . '</div>'; ?>
<?php }
add_filter( 'term_description', 'add_class_to_term_description' ); ?>
im getting and empty cell class returned hundreds of times which is exhausting the memory. what am i doing wrong?
trying to filter the output of term_description to include a class https://codex.wordpress/Function_Reference/term_description
function add_class_to_term_description() { ?>
<?php echo '<div class="cell">' . term_description() . '</div>'; ?>
<?php }
add_filter( 'term_description', 'add_class_to_term_description' ); ?>
im getting and empty cell class returned hundreds of times which is exhausting the memory. what am i doing wrong?
Share Improve this question asked May 27, 2016 at 11:43 Daniel FloridoDaniel Florido 951 silver badge8 bronze badges 2 |4 Answers
Reset to default 1this does the trick
<?php function add_class_to_term_description($term_description) {
echo '<div class="cell">' . $term_description. '</div>';
}
add_filter( 'term_description', 'add_class_to_term_description' ); ?>
Daniel is on the right track, using the value that is passed into the function. However, you should not be echoing anything because this is a filter. You should instead return the value.
<?php
function add_class_to_term_description( $term_description ) {
return '<div class="cell">' . $term_description. '</div>';
}
add_filter( 'term_description', 'add_class_to_term_description' ); ?>
you have to use passed $value
instead of term_description()
function:
function add_class_to_term_description() {
echo '<div class="cell">' . $value . '</div>';
}
add_filter( 'term_description', 'add_class_to_term_description' );
I tested many potential solutions, including all the answers above. They either did not work or did not work as expected - some output empty paragraphs, other do nothing. The best solution for me was to use the PHP function preg_replace to perform a search for the opening paragraph HTML tag and to insert the class attribute with the value I need like this:
function add_class_to_term_descr($content){
return preg_replace('/<p([^>]+)?>/', '<p$1 class="lead">', $content, 1);
}
add_filter('term_description', 'add_class_to_term_descr');
This solution can be easily modified to match other types of WordPress output, like "div" tag for example. I based my solution on this answer and modified it according to my needs.
本文标签: filtersadd class to termdescription
版权声明:本文标题:filters - add class to term_description 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741328895a2372654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
term_description
exists? I just couldn't find it online.. If it does, to proper way to apply a filter to it isreturn
instead ofecho
, but you're missing what to filter as the function's first param.add_class_to_term_description( $term_description ) { return '<div class="cell">' . $term_description . '</div>'; }
THAT if the hook exists. – Ismail Commented May 27, 2016 at 11:54<div class="term-description"><div class="cell"><p>content</p></div></div>
so im thinking i need to use a filter to remove the container div. something likeremove_filter($term_desciption, 'the_wrapper_div');
– Daniel Florido Commented May 27, 2016 at 23:05