最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Counting posts and trigger it

programmeradmin1浏览0评论
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 question

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>  
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 question

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>  
Share Improve this question asked Feb 15, 2021 at 12:01 Mo BoMo Bo 31 bronze badge 1
  • This can be done with a simple 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
Add a comment  | 

1 Answer 1

Reset to default 0

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"

发布评论

评论列表(0)

  1. 暂无评论