WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. design_dolphin
    Member
    Posted 5 years 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 5 years 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>
  3. krusch
    Member
    Posted 4 years ago #

    For some odd reason, I was not able to get the logic behind this script. It seemed to work perfectly in the beginning, but the more posts and tags I had, the more obscure the results were.

    I found another script which addresses the same problem here: http://www.3mind.at/2009/05/06/code-highlighting/

    Maybe it helps somebody.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.