HomeHow To How to Show Limited Number of Tags after Posts in your WP Theme

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

How To Show Limited Number Of Tags After Posts In Your WordPress Theme
How To Show Limited Number Of Tags After Posts In Your WordPress Theme

In this post lets start for how to show limited number of tags when after post in your WordPress Theme.

Actually when we are posting new article in the posts and we adding also number of few tags in the post but in our designing part we want to show limited or one number of tags not to all, So here in this article you can get some idea about how we can limit number of posts tags.

First of all you need to open your theme’s functions.php file and add this code.

add_filter(‘term_links-post_tag’,’show_limit_two’);
function show_limit_two($terms) {
return array_slice($terms,0,2,true);
}

Note: You can change the 2 number to maximum count per your needs.

Now add this code inside your designing part wherever you want.

<?php the_tags() ?>

The code above will show number of 2 limit tags, if you want to less or more tags then just change the number 2 or add this code elsewhere inside your designing section and customize the code.

<?php $posttags = get_the_tags();
$count=1;
if ($posttags) {
foreach($posttags as $tag) {
$count++;
echo ”

“; echo $tag-> name . “

“;
if( $count > 1 ) break;
}
}
else {
echo ”

Main News

“;
}
?>

The above code will be display 1 tags in the theme or designing on your part. If you want to show less tags or more tags, simply adjust the $count > 1 line with the number how you want.

In the end

I hope you have found this article helpful. Let us me your opinion or questions if any through the comment form in below or use this form to ask your question.

You May Also Like

About the Author: Kedar Adhikari

I am WordPress Themes developer and designer and also any type of website design with minimum cost. for more information you can contact me on email [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2024 My WP Helper. All rights reserved.