开发者

limit characters from <?php the_tags ?>

I have a layout that have to show the tags horizontally. I want to be able to limit the amount of characters outputted.

Example - if I set the limit to 14 the following should happen.

Original: Cats, Dogs, Rain

New output: Cats, Dogs, Ra..

Please note that <?php the_tags ?> returns an array. It is everything returned I want limited to 14 characters.

UPDATE In order to remove any confusion I have updated the post with a screenshot displaying why I want to set this limit. This should make it more clear what kind of solu开发者_高级运维tion I am searching for alt text http://img686.imageshack.us/img686/2253/linit.png


substr

Check it out http://www.php.net/manual/en/function.substr.php

Something like this should work for you:

$tags = implode(', ', $the_tags)
echo substr($tags, 0, 14) . (strlen($tags) > 14) ? '..' : '';

substr will handle just showing 14 characters, then the last part appends the .. when needed.


How about using the substr function:

$string_new = substr($string, 0, 14);
echo $string_new;

You might want to also use the strip_tags function if you there are html tags coming in between your text.


This is not very beautiful to cut the words in the middle.
This function will cut your phrase afrer the end of word, but not longer cpecified length.

function short($txt,$length)
{
    $txt = trim(strip_tags($txt));
    if(strlen($txt) > $length)
    {
        $txt = substr($txt,0,$length);
        $pos = strrpos($txt, " ");
        $txt = substr($txt,0,$pos);
        $txt .= "...";
    }
    return $txt;
}

But all answers before are right too.


The trouble with all these answers is that it has not been made clear that the_tags() returns an array of HTML links!

Running substr(implode(', ', get_the_tags(), X) might return something like;

<a href="/tag1/">Tag 1</a>, <a href="/tag2/">Tag 2</a>, <a href="/tag3

You'd be much better off opting for a CSS / front-end solution (as @Gumbo mentioned in the original comments).

That way, you'll still have your link juice and accessibility marked up, without ending up with something like;

<a href="/keyword-tag/">Keyword Tag</a>, <a href="/awesome-keyword/">Awes...</a>


This should really rebuild the whole chain of the_tags, get_the_tag_list and get_the_term_list, but here's a solution in a single function.

It's based on WordPress' get_the_term_list function in wp-includes/category-template.php.

If no trim_length is specified, it hands off to the_tags. HTML entities were decoded so string counts would be accurate, then re-encoded into the output string.

I wasn't completely clear about the need for $before, $sep and $after in the last the_tags filter, so I deferred to what WordPress was already doing there.

function the_tags_trimmed( $before = null, $sep = ', ', $after = '', $trim_length = -1, $trim_characters = '...' ) {
    if ( $trim_length < 1 )
        return the_tags( $before, $sep, $after );

    if ( null === $before )
        $before = __('Tags: ');

    $tags = get_the_terms( 0, 'post_tag' );

    if ( empty( $tags ) )
        return false;

    $html_length = 0;
    $x = 0;

    foreach ( $tags as $tag ) {
        $link = get_term_link( $tag, 'post_tag' );
        if ( is_wp_error( $link ) )
            return $link;
        $tag->name = html_entity_decode( $tag->name );
        if ( strlen($tag->name) + $html_length > $trim_length )
            $tag->name = substr( $tag->name, 0, $trim_length - $html_length) . $trim_characters;
        $tag_links[] = '<a href="' . $link . '" rel="tag">' . htmlentities($tag->name) . '</a>';
        $html_length += strlen($tag->name);
        if ( $x++ < count( $tags ) - 1 )
            $html_length += strlen( $sep );

        if ( $html_length >= $trim_length )
            break;
    }

    $tag_links = apply_filters( "term_links-post_tag", $tag_links );
    $tag_list = $before . join( $sep, $tag_links ) . $after;
    echo apply_filters( 'the_tags', $tag_list, $before, $sep, $after );
}

This was lightly tested and should accomplish the @Thomas was asking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜