1 回答

TA貢獻1831條經驗 獲得超4個贊
您需要在 中添加另一個 if
條件來檢查當前用戶是否有任何帖子。if ( is_user_logged_in() )
如果您首先分解想要執行的操作的邏輯,這可以幫助您計劃如何編碼,例如
如果用戶已登錄:
如果他們有帖子則顯示帖子
否則顯示“無帖子”
else: 顯示“無帖子”
現在在您的代碼中應用該邏輯:
<?php
// IF USER IS LOGGED IN
if ( is_user_logged_in() ):
global $current_user;
wp_get_current_user();
$author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
// IF THEY HAVE POSTS, DISPLAY THE POSTS
if ($author_posts->have_posts()):
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<p><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<span class="date"><?php the_time('F j, Y'); ?></span>
</a></p>
<?php endwhile; ?>
<?php
// ELSE (IF THEY HAVE NO POSTS) DISPLAY MESSAGE
else : ?>
<p>No posts</p>
<?php endif; ?>
<?php
// ELSE (IF THE USER IS NOT LOGGED IN) DISPLAY MESSAGE
else : ?>
<p>No posts</p>
<?php endif; ?>
- 1 回答
- 0 關注
- 146 瀏覽
添加回答
舉報