如果用户有一定数量的帖子,do/don不做某事

If user has certain number of posts, do/don't do something

我有一个自定义 post 类型,用户可以使用我创建的按钮从前端提交 post。我希望此按钮仅在他们提交的 post 数量少于 x 时显示,否则它会告诉他们不能再提交了。

我正在使用此代码为当前用户计算 posts 并回显结果。

        <?php 
        $authorid = get_current_user_id();
        query_posts(array( 
            'post_type' => 'home',
            'author' => $authorid,
        ) ); 
            $count = 0;
            while (have_posts()) : the_post(); 
                $count++; 
            endwhile;
            echo 'Homes registered: ' . $count;
        wp_reset_query();?>

效果很好。如何创建类似于下面的代码来实现此条件按钮?

if count=<5 echo 'button'
else
'Sorry youve already submitted too many, greedy'

虽然有几种方法可以做到这一点,但最简单的方法也许就是简单地使用 count_user_posts():

if ( count_user_posts($authorid, 'home') < 5 ) {
    // Show the button
} else {
    echo 'Sorry youve already submitted too many, greedy';
}

您还应该能够使用类似以下内容来计算查询返回的帖子数:$wp_query->post_count,而不是进行另一个函数调用。