WP将前三个字换行

我想过滤the_content以将第一段的前三个单词包装在span标记中。到目前为止, 这是我的代码:

function first_paragraph($content) {
   if(is_single()) {
      return preg_replace('regex goes here', "<p><span>$1</span>", $content, 1);
   }
}

add_filter('the_content', 'first_paragraph');

到目前为止, 我还没有找到与第一段的前三个单词相匹配的正则表达式和替换。

有什么帮助吗?

谢谢!


#1


像这样:

php > $content = 'The quick brown fox jumped over the lazy dog.';
php > echo preg_replace('/^((\S+\s+){2}\S+)/', '<span>$1</span>', $content);
<span>The quick brown</span> fox jumped over the lazy dog.

因此, 你需要这样做:

function first_paragraph($content) {
   if(is_single()) {
      return preg_replace('/^((\S+\s+){2}\S+)/', "<p><span>$1</span>", $content, 1);
   }
}

add_filter('the_content', 'first_paragraph');

请记住, 如果$ content少于四个单词, 则正则表达式将不匹配, 并且替换也不会发生。

来源:

https://www.srcmini02.com/66448.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37089.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?