基本上,我需要从Wordpress内容中删除图库快捷代码,我正在使用
echo preg_replace('/\[gallery ids=[^\]]+\]/', '', get_the_content() );它是删除画廊短代码成功,但也是段落标签,我需要保留。我的想法是,我想输出内容中除图库之外的所有内容。
发布于 2013-08-15 23:26:06
您可以使用Wordpress strip_shortcode function。
请看Codex中的示例。您可以创建剥离快捷代码的筛选器:
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}并在需要时调用它(在模板中):
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')编辑1个
另一种方法是(回答你的评论)你可以在删除不受欢迎的快捷代码后在内容中使用Wordpress apply_filters function。
//within loop
$content = get_the_content();
$content = preg_replace('/\[gallery ids=[^\]]+\]/', '', $content );
$content = apply_filters('the_content', $content );
echo $content;但我不会建议你这么做。我认为强迫你的网站修改帖子的内容可能会让人很难理解。也许你应该使用Wordpress Excerpt,避免任何问题。
发布于 2013-12-27 15:48:37
要删除短代码或特定的短代码列表,可以使用此代码。
global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* @global int $remove_shortcode
* @param type $shortcodes comma seprated string, array of shortcodes
* @return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;
if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;
$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
dot1_strip_shortcode( 'gallery' );接受单个逗号分隔的短代码字符串或短代码数组。
https://stackoverflow.com/questions/18255441
复制相似问题