首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从使用Fusion Builder的wordpress中剥离HTML和快捷代码

从使用Fusion Builder的wordpress中剥离HTML和快捷代码
EN

Stack Overflow用户
提问于 2015-08-26 05:20:56
回答 2查看 2.3K关注 0票数 2

首先,我使用的是Wordpress > Avada Theme >和Fusion Core插件。

我想要做的是将任何帖子的内容缩减到几个字符。除了使用Fusion Core插件的post内容之外,我拥有的几乎所有内容都可以使用。

这是我的函数,它应该得到摘录并缩小它。

代码语言:javascript
运行
复制
function get_my_excerpt_by_id($post_id, $length, $strip_html)
{   
    if(!$length)        
      $excerpt_length = 35;     
    else        
      $excerpt_length = $length;

    if(!isset($strip_html) || $strip_html == true)      
      $strip_html = true;

    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = apply_filters('the_content',$the_post->post_content); //Gets post_content to be used as a basis for the excerpt

    if($strip_html == true)
        $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images

    $words = explode(' ', $the_excerpt, $excerpt_length + 1);

    if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '…');
        $the_excerpt = implode(' ', $words);
    endif;

    return $the_excerpt; 
}

所以这就像是一个咒语,除非我使用Fusion Core插件作为post类型之一,因为我会得到这样的结果

‘.’.fusion fullwidth-4{padding: 0px !important;…‘

你知道为什么不去掉短码、css和html吗?虽然在看它的时候,可能只是css?那就是留下来。谢谢!

EDIT::所以事实证明,只有css留了下来。因此,现在我必须弄清楚如何删除它们。一段时间后的一切……

EN

回答 2

Stack Overflow用户

发布于 2015-08-26 22:50:17

我只是想在这里发布我的答案,以防任何人稍后需要它,感谢Ghulam Ali对这个答案的帮助。

所以事实证明,当使用Fusion Core时,它添加了<style>标签,当使用strip_tags删除这些标签时,会将css代码嵌套在标签中。

因此,为了解决这个问题,我们首先删除所有标记,减去<style>,使用

$the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>');

然后,我们使用这个漂亮的正则表达式去掉<style>标记及其内容。(我没有写,但不知道如何给写的人以正确的信任)。

$the_excerpt = preg_replace("|<style\b[^>]*>(.*?)</style>|s", "", $the_excerpt);

这是我用来获取内容并从中输出摘录的完整函数。我希望这对某些人有用!

代码语言:javascript
运行
复制
function get_my_excerpt_by_id($post_id, $length, $strip_html){
    if(!$length)
        $excerpt_length = 35;
    else
        $excerpt_length = $length;

    if(!isset($strip_html) || $strip_html == true)
        $strip_html = true;

    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = apply_filters('the_content',$the_post->post_content);//$the_post->post_content); //Gets post_content to be used as a basis for the excerpt

    if($strip_html == true)
    {
        $the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>');
        $the_excerpt = preg_replace("|<style\b[^>]*>(.*?)</style>|s", "", $the_excerpt);
    }

    $words = explode(' ', $the_excerpt, $excerpt_length + 1);

    if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '…');
        $the_excerpt = implode(' ', $words);
    endif;

    return $the_excerpt;
}
票数 2
EN

Stack Overflow用户

发布于 2015-08-26 21:44:07

将样式标签添加到条带标签中

代码语言:javascript
运行
复制
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>'); //add tags like

我希望这能解决问题。

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

https://stackoverflow.com/questions/32214310

复制
相关文章

相似问题

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