首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有人有一个很好的功能,删除一个特定的短代码在Wordpress?

问有没有人有一个很好的功能,删除一个特定的短代码在Wordpress?
EN

Stack Overflow用户
提问于 2013-11-16 19:57:44
回答 1查看 504关注 0票数 3

我正在设置一个新的页面模板,该模板在一个列中显示一个页面的图库(如果它有一个),包含在一个独立的div中,浮动到右边,所有其他内容都向左浮动。我可以通过get_post_gallery()回显右边的图片库,现在我想从the_content()中去掉该图库。

我本质上希望定位的是一个与strip_shortcodes()完全相同的函数,但是用于特定的短代码。类似于strip_shortcode(“图片库”)或strip_shortcode(“图片库”,content())。有人为Wordpress写了这样一个函数吗?

Remove_shortcode(“图库”)工作条--它在运行时会将该死的短代码文本留在后面。我可以通过CSS隐藏图库,也可以通过jQuery删除它,但我希望它一开始就不被输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-27 07:32:49

若要删除短代码或特定的短代码列表,可以使用此代码。

代码语言:javascript
复制
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( 'Test' );
dot1_strip_shortcode( 'Test, new_shortcode' );
dot1_strip_shortcode( array('Test', 'new_shortcode') );

接受单个、逗号分隔的短代码字符串或短代码数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20023106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档