亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

顯示“沒有帖子”如果用戶已登錄但還沒有帖子,Wordpress 中會顯示消息

顯示“沒有帖子”如果用戶已登錄但還沒有帖子,Wordpress 中會顯示消息

PHP
明月笑刀無情 2023-12-15 15:23:58
我目前正在使用下面的代碼。它顯示當前登錄用戶發布的所有帖子的鏈接。我想顯示“沒有帖子”如果當前登錄的用戶還沒有帖子,則顯示消息。感謝所有的幫助。<?php 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);while($author_posts->have_posts()) : $author_posts->the_post();?><p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p><?php endwhile; ?><?php else : ?><p>No posts</p><?php endif; ?>
查看完整描述

1 回答

?
慕容708150

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; ?>


查看完整回答
反對 回復 2023-12-15
  • 1 回答
  • 0 關注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號