如果用戶沒有帖子,我嘗試隱藏鏈接。 我嘗試了很多片段,但仍然沒有工作?如何隱藏鏈接,如果用戶沒有帖子
下面是我試圖隱藏鏈接:<a class="btn btn-success author-link" href="<?php $user_info = get_currentuserinfo(); echo esc_url(get_author_posts_url($user_info->ID)); ?>" rel="author">View all posts</a>
編輯: 我想我找到的東西:
<?php function count_userposts($userid)
{$args = array(
'numberposts' => -1,
'post_type' => array('post', 'article_type'),
'post_status' => 'publish',
'author' => $userid
);
$counter_posters = count(get_posts($args));
return $counter_posters; } ?>
<?php if(count_userposts(wp_get_current_user()->ID)) { ?>
<a class="btn btn-success author-link" href="<?php $user_info = get_currentuserinfo(); echo esc_url(get_author_posts_url($user_info->ID)); ?>" rel="author">View all posts</a>
<?php } else { ?>
b
<?php } ?>
在此先感謝,
你需要添加更多關於帖子的代碼。我認爲'如果(count($ allPosts)> 0){//在這裏寫你的代碼}'但問題是你在哪裏定義帖子 – devpro
[計數作者帖子](https://codex.wordpress.org/Function_Reference/count_user_posts ),並做'如果($ user_post> 0)' – Anish
謝謝,我想我找到了一種方法來隱藏鏈接!我不知道這是否是最好的解決方案,但它的工作原理! –