我想使用PHP子字符串函数在wordpress中显示限制内容。我使用此代码PHP中的子字符串函数不工作
<?php $content = get_the_content(); echo substr($content, 50); ?>
,但它无法正常工作。它显示全部内容。
我想使用PHP子字符串函数在wordpress中显示限制内容。我使用此代码PHP中的子字符串函数不工作
<?php $content = get_the_content(); echo substr($content, 50); ?>
,但它无法正常工作。它显示全部内容。
使用下面的代码可以在此帮助您
<?php
$content = get_the_content();
echo substr($content, 0, 50);
?>
试试这个代码
$content = get_the_content();
echo substr($content, 0, 50);
或者更好的检查长度:
$content = get_the_content();
if (strlen($content) > 50)
$content = substr($content, 0, 50);
echo $content;
在功能:
echo mytruncate(get_the_content());
function mytruncate($text, $length=50) {
if (strlen($text) > $length)
return substr($text, 0, $length);
return $text;
}
@kingkero哈哈:) noIdidNotDoItOnPorpouse,JustHurry – FrancescoMM 2014-09-22 10:59:30
我使用此代码,现在它正在
<?php $content = get_the_content(); echo substr($content, 0, 50); ?>
'0' 失踪。
substr中的第一个参数是子字符串应返回的位置。 – 2014-09-22 10:49:06
我宁愿使用'the_excerpt()':http://codex.wordpress.org/Function_Reference/the_excerpt – halloei 2014-09-22 10:51:16