Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:08:20+00:00 2026-06-02T18:08:20+00:00

I seem to be having difficulty. I need to show a specific piece of

  • 0

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.

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 */ ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:08:22+00:00Added an answer on June 2, 2026 at 6:08 pm

    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';
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulty tuning the placement of text on my web page. Items
I seem having difficulty in understanding the reason behind the need of having many
I'm having difficulty mocking the PDO object with PHPUnit. There doesn't seem to be
I seem to be having difficulty with asp:querystringparameter and asp:gridview . I have the
Having difficulty checking (checkbox) all nodes by a specific class here is what i
Seem to be having some issues storing the current date in a core data
I seem to be having caching issues with the ALAssetLibrary when testing my app
I seem to be having a strange problem with one of my database views
I seem to be having an issue with the photo uploading function on a
I seem to be having a problem with this jquery code. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.