BlogThemes

How to Show Limited Number of Tags after Posts in your WordPress Theme

Just lately we had been requested tips on how to show a restricted variety of tags after every submit in your WordPress theme. Usually, you employ a perform the_tags() to show a hyperlink to the tags a submit belongs to. Nonetheless that perform doesn’t have a parameter to restrict the variety of tags displayed. So in case your submit has 12 tags, and your theme solely has area for five, then it may not look so good within the design. Plenty of people simply restrict the utilization of tags, or don’t even embody it within the templates. However on this article, we’ll present you how one can restrict the variety of tags after posts in your WordPress theme with out limiting the variety of tags you add to every submit.

Edit: Apparently after writing this text, essentially the most superior Otto (@otto42) replied on my Google+ account to let me know that there’s a less complicated method of carrying out this.

First you want to open your theme’s features.php file and add this perform:

1
2
3
4
add_filter('term_links-post_tag','limit_to_five_tags');
perform limit_to_five_tags($phrases) {
return array_slice($phrases,0,5,true);
}

You possibly can change the 5 quantity to most depend you need.

Then open your loop.php, single.php, index.php, or wherever you need to add these submit tags (have to be inside a submit loop), then paste the next code:

1
<?php the_tags() ?>

That is positively rather a lot less complicated than what I had provide you with which I’ll go away on this submit for individuals who care.

Outdated Sophisticated Methodology

All you want to do is paste the next code in your theme file (contained in the submit loop):

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$posttags = get_the_tags();
$depend=0; $sep='';
if ($posttags) {
    echo 'Tags: ';
    foreach($posttags as $tag) {
        $depend++;
        echo $sep . '<a href="'.get_tag_link($tag->term_id).'">'.$tag->identify.'</a>';
$sep = ', ';
        if( $depend > 5 ) break; //change the quantity to regulate the depend
    }
}
?>

The code above will show 6 tags within the theme. If you wish to present much less tags or extra tags, merely regulate the $depend > 5 line with the quantity you need. Keep in mind, regardless that the depend quantity says better than 5, we see 6 tags. That’s as a result of the depend is beginning at 0. So if you wish to present solely Four tags, then the quantity would should be 3.

If you wish to change the separator, then you want to change line 9. The present code will separate by commas.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBest WordPress PluginsBlog

9 Best Social Proof Plugins for WordPress & WooCommerce (2020)

BlogTutorials

How To Add Pagination To Your WordPress Theme

Tips

9 Things You Should Consider while Choosing a WordPress Theme

Tips

The Dilemma Between “Pretty” And User Friendly: How To Pick The Perfect WordPress Theme

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro