是否可以过滤内容或摘录的输出,以便我只获得短代码输出,并且如果用户输入的短代码多于该短代码,则该页面的其余内容将被删除。
过滤除短码输出外的所有内容
1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成
function shortcode_only_wpse_96114($content) {
$regex = get_shortcode_regex();
preg_match_all(\'/\'.$regex.\'/\',$content,$matches);
if (!empty($matches[0])) {
$content = do_shortcode(implode(\' \',$matches[0]));
}
return $content;
}
add_filter(\'the_content\',\'shortcode_only_wpse_96114\',1);
这应该检查是否有任何注册的短代码,如果存在,则用短代码的输出完全替换帖子内容,删除任何其他内容。如果没有找到短码,则post内容将正常返回。几乎未经测试。可能是马车排空警告不退款。
参考文献
http://codex.wordpress.org/Function_Reference/get_shortcode_regex
结束