2015-08-25 65 views
1

首先,我使用的是Wordpress> Avada Theme>和Fusion Core Plugin。從使用Fusion Builder的wordpress中剝離HTML和短代碼

我想要做的是將任何帖子的內容都刪除爲幾個字符。而且我幾乎所有的東西都用到了,使用Fusion Core插件發佈內容。

這是我的功能,應該得到摘錄和縮小它。

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; 
} 

所以這就像一個魅力除非我正在使用融合的核心插件後類型之一beccause然後我得到這個

」 .fusion-全角-4 {填充左:0像素!重要; ...'

任何想法爲什麼它不剝離短碼和CSS和HTML?雖然看着它,也許它只是CSS?那是停留。謝謝!

編輯:: 所以它證明了它只是留下的CSS。所以現在我必須弄清楚如何刪除它們。一段時間後一切....

回答

1

附加樣式標籤到帶標籤

$the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>'); //add tags like 

我希望這將解決。

+0

這實際上是現在strip_tags的工作原理。上面的標籤是可允許的標籤,所以這些標籤將被跳過。但是,謝謝你花時間去幫助我。 – Bardsworth

+0

是否要從內容中刪除短代碼?你嘗試過preg_replace功能嗎? –

+0

更改爲get_the_content時,我根本沒有返回任何內容。那裏有什麼不同? – Bardsworth

1

我只是想在這裏發佈我的答案,以備日後有人需要時,謝謝古拉姆阿里,對這個答案的幫助。

所以事實證明,使用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);

這是全功能我用它來得到它的內容和輸出的摘錄。我希望這對於某人來說很方便!

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; 
}