我知道两个很好的技巧,可以很好地处理短代码,也可以处理摘录。
您可以使用以下方法分析摘录的内容:
function my_excerpt( $content ) {
/* Parse nested shortcodes and add formatting. */
$content = trim( wpautop( do_shortcode( $content ) ) );
/* Remove \'</p>\' from the start of the string. */
if ( substr( $content, 0, 4 ) == \'</p>\' )
$content = substr( $content, 4 );
/* Remove \'<p>\' from the end of the string. */
if ( substr( $content, -3, 3 ) == \'<p>\' )
$content = substr( $content, 0, -3 );
/* Remove any instances of \'<p></p>\'. */
$content = str_replace( array( \'<p></p>\' ), \'\', $content );
return $content;
}
此外,还有使用[原始]短代码的技术:
function my_formatter($content) {
$new_content = \'\';
$pattern_full = \'{(\\[raw\\].*?\\[/raw\\])}is\';
$pattern_contents = \'{\\[raw\\](.*?)\\[/raw\\]}is\';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter(\'the_content\', \'wpautop\');
remove_filter(\'the_content\', \'wptexturize\');
add_filter(\'the_content\', \'my_formatter\', 99);
我希望这有帮助。
如果你想让我们帮助你完成你的功能,首先说一下功能有什么问题总是一个好主意?