限制post_meta (wordpress)中的字符


Limit characters in post_meta (wordpress)

如何限制此代码中的字符?

.get_post_meta($post->ID,'smple1',true).

我试过了:

$trim_length = 21;  //desired length of text to display 
$custom_field = 'smple1';  
$value = get_post_meta($post->ID, $custom_field, true);    
if ($value) {    
  echo '.rtrim(substr($value,0,$trim_length)) . ';  
}

但是我得到服务器错误。我一定是漏掉了什么结尾?

许多谢谢。

您需要获取元值,确保它在false中,然后使用substr()来获得21个字符,如果基于strlen()的结果字段较短,则更少

$length = 21;
$custom_field = 'smple1';
$value = get_post_meta( $post->ID, $custom_field, true );
if ( false !== $value ){
    echo substr( $value, 0, (strlen($value) < $length)? strlen($value) : $length );
}

使用自定义元键值显示字符编号:

<?php
      $trim_length = 21;
      $custom_field = 'lp_sub_description';  
      $value = get_post_meta($post->ID, $custom_field, true);    
      if ($value) {    
        $data = rtrim(substr($value,0,$trim_length));
        echo $data;  
       } 
?> 

也可以使用简单的代码:

<?php echo substr(get_post_meta($post->ID, 'lp_sub_description', true), 0, 21); ?>

经过多次测试,它在我的情况下工作如下:

$filetext = (get_post_meta($post->ID, "smple1", true));
'.substr($filetext, 0, 8) . "...".'

注意什么时候放'. and .'