在没有html标签的情况下发布第一段

时间:2017-08-05 作者:Onuwa O

请有人能帮我正确的代码,以过滤没有任何形式的html标记在源代码中的任何帖子的第一段。

我使用了函数get\\u first\\u paragration(){global$post;$str=wpautop(get\\u the\\u content());$str=substr($str,0,strpos($str,\'

\')+4)$str=条带标签($str,“”);返回“”$str.”

“;}

在函数中。php文件

在标题中,我想显示它,但它留下了一个空白,没有显示任何文本。

请帮忙,我会很感激的。提前感谢

1 个回复
SO网友:Nuno Sarmento

您可以使用wp\\u trim\\u extract创建函数来获取文本的第一段并忽略图像

function custom_excerpt($text, $raw_excerpt) {
  if( ! $raw_excerpt ) {
  $content = apply_filters( \'the_content\', get_the_content() );
  $text = substr( $content, 0, strpos( $content, \'</p>\' ) + 4 );
  }
  // strip any images on the code 
  $text = preg_replace("/<img[^>]+\\>/i", "", $text); 
  return $text;
add_filter( \'wp_trim_excerpt\', \'custom_excerpt\', 10, 2 );

结束

相关推荐