3

是否可以在何时执行之前从内容中删除图库短代码the_content()?我搜索了 codex 并找到了remove_shortcode( $tag ),但它们没有显示任何示例。

我尝试添加功能

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

它不起作用..

更新:

我可以使用下面的代码取消注册短代码,但它也会删除内容

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

4 回答 4

6

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

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

如果你问我最简单的方法..

于 2012-09-28T19:58:03.017 回答
5

奇怪的。remove_shortcode ( codex link ) 不接受第二个参数。

您返回的是 remove_shortcode 函数的 true 或 false 返回,而不是删除了短代码的内容。

在你的函数的第二个版本中尝试这样的事情:

remove_shortcode('gallery');
return $content;

或者只是把

 remove_shortcode('gallery');

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

于 2011-12-18T04:23:39.980 回答
2

一个老问题,但经过一些挖掘和综合答案后,这对我有用:

<?php $content = get_the_content();

echo strip_shortcodes($content);?>

我只是插入画廊,我想将其删除并单独显示。显然,如果您想删除特定的短代码,这可能不是您的解决方案。

于 2015-01-26T22:02:07.107 回答
2

我认为您应该像这样使用子字符串替换:

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

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

更新:您可以通过添加代码取消注册 function.php 中的镜头代码:

remove_shortcode('[gallery]');
于 2011-12-17T21:24:27.170 回答