要仅在生成自动摘录时过滤内容,必须链接挂钩:
钩住get_the_excerpt
并为注册筛选器the_content
.在第二个过滤器中,在摘录可以看到之前,删除两个元素及其内容示例:
add_filter( \'get_the_excerpt\', \'t5_excerpt_clean_up\', 1 );
/**
* Register handler for auto-generated excerpt.
*
* @wp-hook get_the_excerpt
* @param string $excerpt
* @return string
*/
function t5_excerpt_clean_up( $excerpt )
{
if ( ! empty ( $excerpt ) )
return $excerpt;
add_filter( \'the_content\', \'t5_excerpt_content\' );
return $excerpt;
}
/**
* Strip parts from auto-generated excerpt.
*
* @wp-hook the_content
* @param string $content
* @return string
*/
function t5_excerpt_content( $content )
{
// Remove immediately; maybe the next post doesn\'t
// use an excerpt, but the full content.
remove_filter( current_filter(), __FUNCTION__ );
// Fails with nested tables. Just don\'t do that. :)
return preg_replace( \'~<(pre|table).*</\\1>~ms\', \'\', $content );
}