首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress图库短码预替换

问Wordpress图库短码预替换
EN

Stack Overflow用户
提问于 2013-08-15 22:57:51
回答 2查看 2.1K关注 0票数 1

基本上,我需要从Wordpress内容中删除图库快捷代码,我正在使用

代码语言:javascript
复制
echo preg_replace('/\[gallery ids=[^\]]+\]/', '',  get_the_content() );

它是删除画廊短代码成功,但也是段落标签,我需要保留。我的想法是,我想输出内容中除图库之外的所有内容。

EN

回答 2

Stack Overflow用户

发布于 2013-08-15 23:26:06

您可以使用Wordpress strip_shortcode function。

请看Codex中的示例。您可以创建剥离快捷代码的筛选器:

代码语言:javascript
复制
function remove_shortcode_from($content) {
  $content = strip_shortcodes( $content );
  return $content;
}

并在需要时调用它(在模板中):

代码语言:javascript
复制
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')

编辑1个

另一种方法是(回答你的评论)你可以在删除不受欢迎的快捷代码后在内容中使用Wordpress apply_filters function。

代码语言:javascript
复制
//within loop
$content = get_the_content();
$content = preg_replace('/\[gallery ids=[^\]]+\]/', '',  $content );
$content = apply_filters('the_content', $content );
echo $content;

但我不会建议你这么做。我认为强迫你的网站修改帖子的内容可能会让人很难理解。也许你应该使用Wordpress Excerpt,避免任何问题。

A link that helped me

票数 3
EN

Stack Overflow用户

发布于 2013-12-27 15:48:37

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

代码语言: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( 'gallery' );

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

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

https://stackoverflow.com/questions/18255441

复制
相关文章

相似问题

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