php - 以特定帖子类型的完整句子结束摘录
问题描述
对于我网站上的特定帖子类型,我试图使摘录以句子结尾,但由于某种原因,它也会影响页面摘录,我不明白为什么。
function vhr_variable_length_excerpt($text, $w_length, $finish_sentence){
global $post;
if ( $post->post_type == 'poi' ) {
//Word length of the excerpt. This is exact or NOT depending on your '$finish_sentence' variable.
$w_length = 20; /* Change the Length of the excerpt. The Length is in words. */
//1 if you want to finish the sentence of the excerpt (No weird cuts).
$finish_sentence = 1; // Put 0 if you do NOT want to finish the sentence.
$tokens = array();
$out = '';
$word = 0;
//Divide the string into tokens; HTML tags, or words, followed by any whitespace.
$regex = '/(<[^>]+>|[^<>\s]+)\s*/u';
preg_match_all($regex, $text, $tokens);
foreach ($tokens[0] as $t){
//Parse each token
if ($word >= $w_length && !$finish_sentence){
//Limit reached
break;
}
if ($t[0] != '<'){
//Token is not a tag.
//Regular expression that checks for the end of the sentence: '.', '?' or '!'
$regex1 = '/[\?\.\!]\s*$/uS';
if ($word >= $w_length && $finish_sentence && preg_match($regex1, $t) == 1){
//Limit reached, continue until ? . or ! occur to reach the end of the sentence.
$out .= trim($t);
break;
}
$word++;
}
//Append what's left of the token.
$out .= $t;
}
return trim(force_balance_tags($out));
}
}
function vhr_excerpt_filter($text){
global $post;
if ( $post->post_type == 'poi' ) {
//Get the full content and filter it.
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
//If you want to Allow SOME tags:
$allowed_tags = '<p>,<a>,<strong>,<b>'; /* Here I am allowing p, a, strong tags. Separate tags by comma. */
$text = strip_tags($text, $allowed_tags);
//Create the excerpt.
$text = vhr_variable_length_excerpt($text, $w_length, $finish_sentence);
return $text;
}
}
//Hooks the 'vhr_excerpt_filter' function to a specific (get_the_excerpt) filter action.
add_filter('get_the_excerpt', 'vhr_excerpt_filter', 5);
它不会影响我的任何其他自定义帖子类型,只会影响我在函数中指定的类型,然后是网站上的所有页面。即使我将逻辑更改为 = poi && != page 之类的东西,它仍然会影响我的页面。任何想法为什么这也会影响页面?有没有更简单的方法来实现这一点?
谢谢!
解决方案
我们有很多方法可以接近它。花时间编写自定义摘录而不是依赖 Wordpress 就是其中之一......
我们可以通过瞄准句末期来计算句子。
function get_sentence_tally_excerpt( $content = '', $tally = 2, $stitches = '' ) {
$buffer = array_slice( explode( '.', sanitize_text_field( $content ) ), 0, $tally );
$filter = array_filter( array_map( 'trim', $buffer ), 'strlen' );
$excerpt = join( '. ', $filter ) . '.';
return esc_attr( $excerpt . $stitches );
};
您可以指定应该“截断”什么类型的内容以及多少个句子。在前端,我们可以get_tally_excerpt()
这样调用我们的函数:
<?= get_sentence_tally_excerpt( get_the_content() ); ?> //... 2 sentences by DEFAULT
<?= get_sentence_tally_excerpt( get_the_content(), 1 ); ?> //... 1 sentences ONLY
<?= get_sentence_tally_excerpt( get_the_content(), 5, '[...]' ); ?> //... 5 sentences ONLY with stitches at the end.
推荐阅读
- android - 科尔多瓦天蓝色 adal 身份验证重定向 URI 设置为 file:///
- python - 带变量的中断循环 - Python
- azure - 我的 Azure 函数如何选择性地使用来自服务总线的消息?
- azure - LogicApp 在 Microsoft Teams 中发帖
- rest - 如何将多个 yaml 文件转换为单个整体 yaml 文件?
- azure - Azure API 管理 JWT 验证失败
- c# - Show multiple table data in View ASP.NET
- mysql - 站点定期出现错误“Mysql 连接错误 [localhost]:(2002)连接被拒绝(400)”
- ios - iOS:在没有情节提要的情况下以编程方式为阿拉伯语和希伯来语类语言翻转整个应用程序 UI
- python-2.7 - 如何存储 Selenium WebDriver 中存在的 webdriverObject.get_attribute("srcset") 的返回值?