Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Center area post/pages (http://forum.bytesforall.com/forumdisplay.php?f=32)
-   -   HOWTO: Display tags with tag count on a post (http://forum.bytesforall.com/showthread.php?t=20563)

Tim F Jun 27, 2013 06:32 PM

HOWTO: Display tags with tag count on a post
 
Hey all,
For a while I've been wanting to display the tags on a post only if there are more than 1 posts with that tag, and showing the total number of posts with that tag. The idea is to encourage site navigation by linking similar posts with tags, and not making people click on a tag only to find no other posts.
So the result would look like this:
View more posts tagged with: Elephants (3), Rabbits (1)

I have thrashed out the following code, which works. It is quite possibly not the most elegant solution so if you can improve it feel free ;) I'm still using 3.5.1 so I just include this in Style & Edit Center Column > The Loop, underneath the post byline.
Code:

<?php
$tags = get_the_tags();
if (is_array($tags)) {
foreach ($tags as $tag) {
if($tag->count > 1) $tagstart = 'Read more posts tagged with:  ';
$tag_link = get_tag_link( $tag->term_id );
if($tag->count > 1) $tagstring .= "<a href='{$tag_link}'>" . $tag->name . ' (' . $tag->count . ')' . "</a>" . ', '
;
};
$tagdisp = $tagstart . $tagstring;
echo rtrim($tagdisp, ', ');
}
;?>



All times are GMT -6. The time now is 02:51 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.