I am trying to figure out how i can install a code for the counting posts. I already have a code for the posts on the website. But i want to trigger it for example if there are no posts, the text should change to no posts found or if there is just one post the text should then be: we found 1 post.
Here is the code i made in the function.php
function wpb_total_posts() {
$total = wp_count_posts()->publish;
echo 'We found', "<strong>" . $total . "</strong>", 'jobs';
}
and this one i made in the loop
<div>
<h4 class="total-posts">
<?php wpb_total_posts(); ?>
</h4>
</div>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Modify your wpb_total_posts function like bellow:
function wpb_total_posts() {
$total = wp_count_posts()->publish;
if($total == 1){
echo 'We found', "<strong>" . $total . "</strong>", 'job';
}elseif($total > 1){
echo 'We found', "<strong>" . $total . "</strong>", 'jobs';
}else{
echo 'No posts found ';
}
}
Here I’m first checking if total amount of post is 1 or more and handling singular/plural accordingly. Lastly if there is no post then rendering “No post found”
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0