2011-12-17 107 views
3

是否有可能在执行the_content()之前从内容中删除图库短代码?我搜查了codex,发现remove_shortcode($tag),但它们没有显示任何示例。Wordpress从内容中删除短代码

我尝试添加到功能

function remove_gallery($content) { 
    $content .= remove_shortcode('[gallery]'); 
    return $content; 
} 
add_filter('the_content', 'remove_gallery', 6); 

它不工作..

更新:

我可以使用下面的代码来注销短代码,但它也消除了content

function remove_gallery($content) { 
    return remove_shortcode('gallery', $content); 
} 
add_filter('the_content', 'remove_gallery', 6); 

回答

5

奇怪。 remove_shortcode(codex link)不需要第二个参数。

您正在返回remove_shortcode函数的true或false返回值,而不是移除shortcode的内容。

尝试要么像这样在你的函数是第二个版本在那里:

remove_shortcode('gallery'); 
return $content; 

或者只是把

remove_shortcode('gallery'); 

在你的functions.php文件。以前的海报建议包括[]的,我猜是错误的。

+0

谢谢!我没有返回内容。如果你可以指定要删除的短代码,'strip_shortcodes()'是最理想的。 – CyberJunkie 2011-12-18 05:51:00

2

I认为你应该使用这样的子字符串替换:

function remove_gallery($content) { 
    return str_replace('[gallery]', '', $content); 
} 
add_filter('the_content', 'remove_gallery', 6); 

请记住,这种方法不具备良好的性能。

更新:您可以通过添加代码注销在function.php的shotcode:

remove_shortcode('[gallery]'); 
+0

谢谢!不幸的是,'str_replace'方法不能注册简码。 – CyberJunkie 2011-12-17 22:25:54

+0

我能够使用过滤器取消注册问题。请参阅上面的更新 – CyberJunkie 2011-12-17 22:29:24

+0

我认为你所做的是不正确的。要删除短代码,只需在function.php中添加以下代码:remove_shortcode('[gallery]'); – bingjie2680 2011-12-18 09:58:13

6

我知道这是一个相对老的问题,但strip_shortcodes函数确实有效!如果你问我

global $post; 
echo strip_shortcodes($post->post_content); 

最简单的方法..

+0

但请记住'strip_shortcodes()'将删除*所有*简码,而不仅仅是'gallery'简码。这个问题只涉及'画廊'的简码。 – Werner 2016-10-16 04:54:19

2

一个老问题,但经过一番挖掘和答案,这为我工作的组合:

<?php $content = get_the_content(); 

echo strip_shortcodes($content);?> 

我只是插入画廊,我想要分开移除和显示。显然,如果你想删除特定的简码,这可能不是你的解决方案。