Showing posts with label limiting amount of tags displayed under posts. Show all posts
Showing posts with label limiting amount of tags displayed under posts. Show all posts

Tuesday, June 4, 2013

Limiting the amount of tags displayed beneath posts in Wordpress

For displaying tags, _s theme uses this function: http://codex.wordpress.org/Function_Reference/get_the_tag_list

This saves me from a few extra lines of php but my goal is to limit the amount of tags displayed under a post.  Now I could always accomplish this in jQuery by hiding the extra ones, but I wouldn't be able to hide the seperators also and this isn't as efficient as through the actual loop.

So this is what I need to use instead: http://codex.wordpress.org/Function_Reference/get_the_tags

And this is what I came up with to limit the tags to only four posts per post:
<?php
$tags_list = get_the_tags();
$tags = 0;
if ( $tags_list ) :
?>
<span class="tags-links">
<?php
foreach ($tags_list as $tag){
if ($tags < 4){
echo '<a href="'. home_url() . '?tag=' . $tag->name . '">' . $tag->name . '</a> ';
}
$tags++;
}
?>
</span>
<?php endif; // End if $tags_list ?>


$tags_list holds all the tags and $tags is used as our counter.  The rest of the code is pretty self explanatory.