3

このコードの文字数を制限するにはどうすればよいですか?

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

しかし、サーバーエラーが発生します。私はendifか何かが欠けているに違いありませんか?

どうもありがとう。

4

3 に答える 3

0

カスタム メタ キーの値を使用して文字数を表示します。

<?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); ?>
于 2013-06-04T12:10:33.943 に答える
0

メタ値をフェッチし、それが含まれていることを確認してから、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 );
}
于 2013-06-03T23:44:10.640 に答える
-1

多くのテストの後、私の場合は次のように機能しました。

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

いつ入れるか注意'. and .'

于 2013-06-04T01:02:06.387 に答える