首页 > 解决方案 > 特定部分奇怪行为的自定义查询

问题描述

我正在为我的 wordpress 网站使用 elementor pro。我在一页上有 4 个部分,即第 1 节、第 2 节、第 3 节和第 4 节。我的默认摘录限制是 100 个字符。我需要在第 2 节中自定义设置 40 个字符的摘录,所以我将查询 ID(set_excerpt_section_2)放在第 2 节中,并具有以下功能:

add_action( 'elementor_pro/posts/query/set_excerpt_section_2', function( $query ) {
    
function globalTruncate( $output ) {
    global $post;
    $chinese_output = preg_match_all("/\p{Han}+/u", $post->post_excerpt, $matches);
    if($chinese_output) {
      $output = mb_substr($output, 0, 40)." ...";
    }
    return $output; 
}

add_filter( 'get_the_excerpt', 'globalTruncate', 10, 2 );

} );

这 4 个部分的摘录出现在页面上:

第 1 部分:100 个字符(正确:默认为 100 个字符)

第 2 部分:40 个字符(正确)

第 3 部分:40 个字符(这应该是默认的 100 个字符)

第 4 节:40 个字符(这应该是默认的 100 个字符)

请提供任何帮助。

标签: wordpresselementor

解决方案


推荐阅读