Show post count in heading of related posts selected by tag in sidebar
-
I wanted to show the related posts of a post in a tab window and also show a post count next to the heading of the tab window.
The code below checks to see whether or not the post has any related posts, excludes the current post, and shows the related posts based on its tags in alphabetical order.
<div> <?php $myposts = get_the_tags($post->ID); $howmany = count($myposts); if ($howmany > 1) { global $post; foreach(get_the_tags($post->ID) as $tag) { $show_tags .= $tag->name . ','; $rel_art .= rtrim($show_tags , ','); } $args = array( 'numberposts' => -1, 'tag'=> $rel_art, 'exclude'=> $post->ID, 'orderby'=> title, 'order'=> ASC,); $myposts = get_posts($args); $howmany = count($myposts); ?> <h2>Related Posts (<?php echo $howmany ;?>)</h2> <div> <?php foreach($myposts as $post) : ?> <ul> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php } else { ?> <p>There are no related posts for this post.</p> <?php } ?> </div> </div>
You will still need to style the css as needed. The above is just for illustration.
Code subject to review.
With many thanks to the people writing the codex and people posting in the forum. 🙂 I hope I wrote everything correctly.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Show post count in heading of related posts selected by tag in sidebar’ is closed to new replies.