首先,我使用的是Wordpress > Avada Theme >和Fusion Core插件。
我想要做的是将任何帖子的内容缩减到几个字符。除了使用Fusion Core插件的post内容之外,我拥有的几乎所有内容都可以使用。
这是我的函数,它应该得到摘录并缩小它。
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留了下来。因此,现在我必须弄清楚如何删除它们。一段时间后的一切……
发布于 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);
这是我用来获取内容并从中输出摘录的完整函数。我希望这对某些人有用!
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;
}发布于 2015-08-26 21:44:07
将样式标签添加到条带标签中
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>'); //add tags like我希望这能解决问题。
https://stackoverflow.com/questions/32214310
复制相似问题