Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI 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>
Closed. This question is off-topic. It is not currently accepting answers.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI 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>
Share
Improve this question
asked Feb 15, 2021 at 12:01
Mo BoMo Bo
31 bronze badge
1
|
1 Answer
Reset to default 0Modify 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"
if
statement, which is beginner level PHP and does not need WordPress specific expertise. What part specifically are you struggling with? If total is more than 1 do X else if total is 1 do Y else do Z – Tom J Nowell ♦ Commented Feb 15, 2021 at 13:27