Show if greater than 3 related articles within same category in wordpress

I seem to be having difficulty.
I need to show a specific piece of text if there are more than or equal to 3 post on a post page template in wordpress. (loop-single.php)

It should be dynamic enough to detect if the total number of post in related category is greater or equal to 3.

Read More

here is a code I found which works well on category template pages(archive.php) but it messes up when I use it in a post template.

<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>

<!-- Less than 3 post - nothing shown at all -->

<?php $count++;
  endwhile; endif; ?>
<?php if ($count > '3') { ?>

<div> This line shown when 3 or more posts are in current post category</div>

<?php } ?>

NOTE: I’m trying to get this to work on the loop-single.php template file.

Any help would be greatly appreciated,
Thank You


Code updated to include above solution. I fixed a few syntax errors, but its now throwing a T-STRING error: Parse error: syntax error, unexpected T_STRING

here is my full page code:

<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php roots_post_before(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php roots_post_inside_before(); ?>
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>

<!-- POST DATE STAMP -->
<div class="post-top">
<div class="date-stamp">
<b><?php the_time('M d'); ?></b>
</div>
</header>

<div class="entry-content">
<?php the_content(); ?>
</div>

<footer>
<hr />


<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{

<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) --> 
<h5>Featured: <?php $cats=get_the_category(); echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p      comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />

}
else
{
Why hello there LESS than three
}
?>



</footer>
<?php comments_template(); ?>
<?php roots_post_inside_after(); ?>
</article>
<?php roots_post_after(); ?>
<?php endwhile; /* End loop */ ?>

Related posts

Leave a Reply

1 comment

  1. This should get you started:

    <?php
    $cat = get_query_var('cat');
    $posts = get_posts(array('category' => $cat));
    if(count($posts) >= 3)
    {
        //CODE EXECUTED IF THREE OR MORE POSTS EXIST IN CURRENT CATEGORY
    }
    else
    {
        //CODE EXECUTED IF LESS THAN THREE POSTS EXIST IN CURRENT CATEGORY
    }
    ?>
    

    EXTRA INFO: The reason why it was failing was because your loop was only performing one iteration. Single posts won’t go through the loop more than once because…. well…… it’s a single post. What this approach does is take the existing category, and queries all WordPress posts in the matching category. Using PHP’s count function will give you the exact number of posts found with the given parameters.

    Word of warning: the script above will not find ALL posts in the matching category. Only the five most recent ones in that given category. If you want an actual total of all matching posts, change one line to the following:

    $posts = get_posts(array('category' => $cat, 'numberposts' => -1));
    

    UPDATES TO CODE: This line:

    <article <?php post_class(); ?> id="post-<?php the_ID(); ?>"> //missing semicolon after post_class()
    

    And this block:

    <?php
    $cats=get_the_category();
    $posts = get_posts(array('category' => $cats[0]->cat_ID));
    if(count($posts) >= 3)
    {
    ?> 
    <!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) --> 
    <h5>Featured: <?php echo $cats[0]->cat_name; ?></h5>
    <div id="foot-container">
    <?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p comments_class=comment-count]"); ?>
    <div style="clear:both;"></div>
    </div>
    <hr />
    <?php
    }
    else
    {
    echo 'Why hello there LESS than three';
    }
    ?>