gpt4 book ai didi

php - 限制 post_meta 中的字符(wordpress)

转载 作者:行者123 更新时间:2023-12-02 05:18:18 25 4
gpt4 key购买 nike

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

.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 之类的东西?

非常感谢。

最佳答案

您需要获取元值,确保它在 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 - 限制 post_meta 中的字符(wordpress),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com