2016-10-09 37 views
-1

如果用戶沒有帖子,我嘗試隱藏鏈接。 我嘗試了很多片段,但仍然沒有工作?如何隱藏鏈接,如果用戶沒有帖子

下面是我試圖隱藏鏈接:<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 } ?> 

在此先感謝,

+0

你需要添加更多關於帖子的代碼。我認爲'如果(count($ allPosts)> 0){//在這裏寫你的代碼}'但問題是你在哪裏定義帖子 – devpro

+0

[計數作者帖子](https://codex.wordpress.org/Function_Reference/count_user_posts ),並做'如果($ user_post> 0)' – Anish

+0

謝謝,我想我找到了一種方法來隱藏鏈接!我不知道這是否是最好的解決方案,但它的工作原理! –

回答

0

下面應該工作: -

<?php 
if(count_user_posts(get_current_user_id()) > 0) : 
?> 
<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 endif; ?> 
0

試試這個,這將會工作..

<?php 
    while (have_posts()): the_post(); 
    // Display post 
    if (have_posts()): // If this is the last post, the loop will start over// Do something if this isn't the last post 
    endif; 
endwhile; 
?> 
相關問題