您可能已经解决了这个问题,但这里有一个示例,说明如何有条件地向单个页面或帖子添加过滤器。
// Check, if custom formatter needs to be added
add_action(
\'template_redirect\',
function() {
// don\'t do anything, if not page or post
if ( ! is_singular( array(\'page\', \'post\') ) ) {
// some other type
return;
}
// targetted page or post
// add other post or page slugs or ID to array
// to use custom formatter on them
$targets = array(
\'some-post-name\', // names/slugs
12, // or IDs
);
// check, that current post/page is one of the targets
global $post;
$current = array($post->ID, $post->post_name);
if ( ! array_intersect( $current, $targets ) ) {
// current ID or slug was not in the targets array
return;
}
// We\'re on a targeted post or page, so let\'s do stuff!
// remove default formatters
remove_filter(\'the_content\', \'wpautop\');
remove_filter(\'the_content\', \'wptexturize\');
// add custom formatter
add_filter(\'the_content\', \'my_formatter\', 99);
}
);
function my_formatter($content) {
// code...
}