Php wordpress。使用substr对类别标题的字符限制

Php wordpress。使用substr对类别标题的字符限制,php,wordpress,Php,Wordpress,我正在使用这个代码。现在我想补充。。。在每一个类别的末尾都有超过10个字符的标题。” 样品:椰子是最好的 怎么做 <a href="' . get_category_link($category->term_id) . '">' . substr($category->cat_name, 0, 10) . ' </a> 您可以先检查字符串长度,查看字符串是否大于10个字符: <?php $current_cat = get_query

我正在使用这个代码。现在我想补充。。。在每一个类别的末尾都有超过10个字符的标题。”

样品:椰子是最好的

怎么做

<a href="' . get_category_link($category->term_id) . '">' . substr($category->cat_name, 0, 10) . ' </a>

您可以先检查字符串长度,查看字符串是否大于10个字符:

<?php
    $current_cat    =   get_query_var('cat');
    $categories     =   get_categories('orderby=id&order=desc&number=16');
    foreach($categories as $category) { ?>
            <div class="latest">
            <?php the_post_thumbnail(array( 160,100 ), array( 'class' => 'primary' )); ?>
                <img src="domain.com/images/<?php echo $category->category_nicename; ?>.jpg" />
            </div>
            <div class="latest-details"><a href="<?php echo get_category_link($category->term_id); ?>"><?php echo substr($category->cat_name, 0, 10); echo (strlen($category->cat_name) > 10)? "...":""; ?></a>
 <?php } ?>
这是斯特伦的手册:


我会这样做的。在该if语句之后设置不带substr的输出,您可以将$category->cat_name保留为对象

您可以使用此函数:

function stringLimit($text,$limit){
       $length = strlen($text);
       if($length>$limit){ 
           $finalText= substr($text,0,$limit)."...";} 
           else {$finalText=$text;}
    return $finalText;
    } 
$text = stringLimit($text,10);

希望这对您有所帮助。

对不起。我真的不明白。这是我的全部代码。伟大的很乐意帮忙。干杯
function stringLimit($text,$limit){
       $length = strlen($text);
       if($length>$limit){ 
           $finalText= substr($text,0,$limit)."...";} 
           else {$finalText=$text;}
    return $finalText;
    } 
$text = stringLimit($text,10);