Forums

Show post count in heading of related posts selected by tag in sidebar (2 posts)

  1. design_dolphin
    Member
    Posted 10 months ago #

    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.

  2. design_dolphin
    Member
    Posted 10 months ago #

    Correction

    The code posted above didn't account for a 0 post count.

    Corrected code:

    <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  { ?>
    
    <h2>Related Posts (0)</h2> 
    
    <div>
    
    <p>There are no related posts for this post.</p>
    
    <?php }  ?>
    
    </div>
    </div>

Reply

You must log in to post.

About this Topic

Tags

No tags yet.