限制post_meta中的字符(wordpress)

时间:2022-10-25 10:47:50

How can I limit the characters in this code?

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

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

I have tried with the following:

我试过以下内容:

$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)) . ';  
}

But I get server error. I must be missing an endif or something?

但我收到服务器错误。我一定错过了一个endif或者其他什么东西?

Many thanks.

非常感谢。

3 个解决方案

#1


0  

You need to fetch the meta value, make sure that it was in false and then use substr() to get the 21 characters, or fewer if the field is shorter based on the results of strlen().

您需要获取元值,确保它为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 );
}

#2


0  

Display no of character using your custom meta key's value:

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

<?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;  
       } 
?> 

Also use simple code :

也使用简单的代码:

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

#3


-1  

Ok after many tests it worked in my case as following:

经过多次测试后,它在我的案例中起作用如下:

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

note when to put '. and .'

注意何时放'。和。'

#1


0  

You need to fetch the meta value, make sure that it was in false and then use substr() to get the 21 characters, or fewer if the field is shorter based on the results of strlen().

您需要获取元值,确保它为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 );
}

#2


0  

Display no of character using your custom meta key's value:

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

<?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;  
       } 
?> 

Also use simple code :

也使用简单的代码:

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

#3


-1  

Ok after many tests it worked in my case as following:

经过多次测试后,它在我的案例中起作用如下:

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

note when to put '. and .'

注意何时放'。和。'