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
  • Are you sure this hook term_description exists? I just couldn't find it online.. If it does, to proper way to apply a filter to it is return instead of echo, 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
  • thanks. its working to some extent. It returns <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 like remove_filter($term_desciption, 'the_wrapper_div'); – Daniel Florido Commented May 27, 2016 at 23:05
Add a comment  | 

4 Answers 4

Reset to default 1

this 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